int bfs(){ int i,j,k; while(!q.empty()){ sqr sq=q.front(); q.pop(); //判断是否到达目标 bool flag = false; for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(sq.a[i][j]!=t[i][j]){ flag = true; } } } if(!flag)return sq.deep; //移动 for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(sq.a[i][j]==0){ for(k=0;k<4;k++){ int x1=i,y1=j,x2=i+d[k][0],y2=j+d[k][1]; if(!check(x2,y2))continue; swap(sq.a[x1][y1],sq.a[x2][y2]); sq.deep++; q.push(sq); swap(sq.a[x1][y1],sq.a[x2][y2]); sq.deep--; } } } } } return INF; }
int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ scanf("%d",&a[j].val); a[j].id=j; } sort(a,a+m,cmp); for(int j=0;j<m;j++){ if(a[j].val==-1) continue; if(!j||a[j].val!=a[j-1].val) cnt++; add(a[j].id,m+cnt+1); add(m+cnt,a[j].id); } cnt++; } for(int i=0;i<m+cnt;i++) if(!in[i]) que.push(i); while(!que.empty()){ int u=que.front();que.pop(); if(u<m) ans[tot++]=u; for(int i=0;i<edge[u].size();i++){ int v=edge[u][i]; in[v]--; if(!in[v]) que.push(v); } } if(tot<m) puts("-1"); else{ for(int i=0;i<m;i++) cout<<ans[i]+1<<(i==m-1?"\n":" "); } return 0; }
void Work() { for (int i = 1; i <= N; i++) dis[i] = INF; memset(use, 0, sizeof(use)); dis[X] = 0; use[X] = true; Q.push(X); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int i = 1; i <= N; i++) { if (Left[v] >= Map[v][i] && dis[v] < dis[i]) { dis[i] = dis[v]; Left[i] = Left[v] - Map[v][i]; if (!use[i]) { use[i] = true; Q.push(i); } } else if (T[v] >= Map[v][i] && dis[v] + C[v] < dis[i]) { dis[i] = dis[v] + C[v]; Left[i] = T[v] - Map[v][i]; if (!use[i]) { use[i] = true; Q.push(i); } } } use[v] = false; } }
void HexBoard::bfs(const ip vertex) { Q.push(vertex); PlayerColor present_player_color; while(!Q.empty()) { ip v=Q.front(); int node=v.first; Q.pop(); markedbfs[node]=presentPlayer; for(int i=0;i<edge[node].size();++i) { int j=edge[node][i]; int row_j=row_val(j); int column_j=column_val(j); if(presentPlayer==1) present_player_color=player1color; else present_player_color=player2color; if(markedbfs[j]==-1 && vertices[row_j][column_j].second==present_player_color) { Q.push(vertices[row_j][column_j]); } } } cout<<endl; //game ends only if same player is on both virtual nodes if(markedbfs[WEST]==presentPlayer && markedbfs[EAST]==presentPlayer && present_player_color==PlayerColor::BLUE) endgame(); if(markedbfs[NORTH]==presentPlayer && markedbfs[SOUTH]==presentPlayer && present_player_color==PlayerColor::RED) endgame(); //reset the markedbfs array markedbfs=(vector<int>(SIZE*SIZE+5,-1)); }
void getFail() { fail[0] = 0; // 初始化队列 for(int c = 0; c < 62; c++) { int u = ch[0][c]; if(u) { fail[u] = 0; //cout<<u<<"("<<endl; q.push(u); } } // 按BFS顺序计算fail while(!q.empty()) { int u = q.front(); q.pop(); for(int c = 0; c < 62; c++) { if(ch[u][c]) { q.push(ch[u][c]); // int v = fail[u]; // while(v && !ch[v][c]) v = fail[v]; // fail[ch[u][c]] = ch[v][c]; // //last[u] = val[f[u]] ? f[u] : last[f[u]]; // val[ch[u][c]]|=val[fail[ch[u][c]]];//计算禁止结点 // continue; fail[ch[u][c]]=ch[fail[u]][c]; val[ch[u][c]]|=val[ch[fail[u]][c]]; } else ch[u][c]=ch[fail[u]][c]; } } }
int main() { int i,n,m,q,ta,tb,sx,sy; scanf("%d %d %d",&n,&m,&q); for(i=0;i<m;i++) {scanf("%d %d",&ta,&tb); f[ta][tb]=-1;} scanf("%d %d",&sx,&sy); inp.x=sx; inp.y=sy; inp.z=1; qu.push(inp); while(not qu.empty()) { inp=qu.front(); qu.pop(); if(inp.x<1||inp.x>n||inp.y<1||inp.y>n||f[inp.x][inp.y]!=0) continue; else { if(inp.z>ma) ma=inp.z; ans[inp.z]++; f[inp.x][inp.y]=inp.z; sx=inp.x; sy=inp.y; ta=inp.z; inp.x=sx-1; inp.y=sy; inp.z=ta+1; qu.push(inp); inp.x=sx+1; inp.y=sy; inp.z=ta+1; qu.push(inp); inp.x=sx; inp.y=sy-1; inp.z=ta+1; qu.push(inp); inp.x=sx; inp.y=sy+1; inp.z=ta+1; qu.push(inp); } } /*for(i=1;i<=n;i++) { for(tb=1;tb<=n;tb++) printf("%d ",f[i][tb]); printf("\n"); }*/ for(i=1;i<=ma;i++) ans[i]+=ans[i-1]; for(i=0;i<q;i++) { scanf("%d",&tb); if(tb>=ma) printf("%d\n",ans[ma]); else printf("%d\n",ans[tb+1]); } return 0; }
// Removes the element on top of the stack. void pop() { if(q1.size() == 1) { q1.pop(); } else{ while(q1.size() != 1) { q2.push(q1.front()); q1.pop(); } q1.pop(); while(!q2.empty()) { q1.push(q2.front()); q2.pop(); } } }
void find_augmenting_path() { init_bfs(); while (!Q.empty()) { int u = Q.front(); Q.pop(); FOR (v, 1, n) if (a[u][v] && match[u] != v && b[u] != b[v]) { if ((v == start || match[v]) && T[match[v]]) blossom_shrink(u, v); else if (!T[v]) { if (!match[v]) { T[v] = u; finish = v; return; } else { T[v] = u; Q.push(match[v]); in_queue[match[v]] = true; } } } } }
void wml::CupsdDirContainer::setDirective(queue<pair<string, string> > containerId, const string& key, const string& value) { bool containerFound = false; if (!containerId.empty()){ pair <string, string> temp = containerId.front(); containerId.pop(); list<CupsdDirContainer>::iterator iter; for (iter = this->directiveContainers.begin(); iter != this->directiveContainers.end(); iter++) { string containerIdString = temp.first + " " + temp.second; if (iter->getId() == containerIdString) { containerFound = true; iter->setDirective(containerId, key, value); } } if (containerFound == false) { throw runtime_error("Container not found 4"); } } else { this->setDirective(key, value); } }
int bfs (int xk0, int yk0, int xk1, int yk1) { if ((xk0 == xk1) && (yk0 == yk1)) return 0; memset(vis, 0, sizeof vis); vis[xk0][yk0] = true; sssp[xk0][yk0] = 0; q.push(make_pair(xk0, yk0)); pair<int, int> act; int x, y, w, z, d; while (!q.empty()) { act = q.front(); q.pop(); x = act.first; y = act.second; for (d=0; d<8; d++) { w = x+disti[d]; z = y+distj[d]; if (check_pos(w, z) && !vis[w][z]) { if ((w == xk1) && (z == yk1)) return sssp[x][y]+1; vis[w][z] = true; sssp[w][z] = sssp[x][y]+1; q.push(make_pair(w, z)); } } } }
void bfs() { while(!q.empty()) { ND now=q.front();q.pop(); //cout<<now.sta<<' '<<now.x0<<' '<<now.y0<<endl; for(int k=0;k<4;k++) { ND tmp; if(!expand(now,tmp,k)) continue; if(tmp.sta==tar) { printf("%d\n",tmp.step); exit(0); } if(!h[tmp.sta]) { h[tmp.sta]=1; q.push(tmp); } } } }
void CReplay :: AddTimeSlot2( queue<CIncomingAction *> actions ) { BYTEARRAY Block; Block.push_back( REPLAY_TIMESLOT2 ); UTIL_AppendByteArray( Block, (uint16_t)0, false ); UTIL_AppendByteArray( Block, (uint16_t)0, false ); while( !actions.empty( ) ) { CIncomingAction *Action = actions.front( ); actions.pop( ); Block.push_back( Action->GetPID( ) ); UTIL_AppendByteArray( Block, (uint16_t)Action->GetAction( )->size( ), false ); UTIL_AppendByteArrayFast( Block, *Action->GetAction( ) ); } // assign length BYTEARRAY LengthBytes = UTIL_CreateByteArray( (uint16_t)( Block.size( ) - 3 ), false ); Block[1] = LengthBytes[0]; Block[2] = LengthBytes[1]; m_CompiledBlocks += string( Block.begin( ), Block.end( ) ); }
int main(){ int t; inp(t); while(t--){ int n,val; inp(n); while(!q.empty()) q.pop(); pairs x=bfs(n); if(x.X.X==-1) cout<<-1<<endl; else { rep(i,x.X.Y) cout<<8; rep(i,x.X.X) cout<<6; cout<<endl; } } return 0; }
void solve() { while(!q.empty()) { int tmp = q.front(); q.pop(); if(!reported[tmp]) { blocked[tmp] = true; continue; } for(int i = 0; i < g[tmp].size(); i ++) if(!used[ g[tmp][i] ]) { used[ g[tmp][i] ] = true; q.push(g[tmp][i]); } } memset(used, 0, sizeof(used)); dfs(1); cout << p - cnt << endl; }
int main() { char s1[3],s2[3]; while(scanf("%s%s",s1,s2)==2){ sx = s1[0] - 'a', sy = s1[1] - '1'; dx = s2[0] - 'a', dy = s2[1] - '1'; while(!x.empty()){ x.pop(); y.pop(); } memset(vis,0,sizeof(vis)); memset(len,0,sizeof(len)); x.push(sx); y.push(sy); if(sx==dx&&sy==dy) printf("To get from %s to %s takes 0 knight moves.\n",s1,s2); else{ bfs(sx,sy); printf("To get from %s to %s takes %d knight moves.\n",s1,s2,sum); } } return 0; }
int bfs() { int code = encode(0, SRC); q.push({code, 0}); vis[code] = true; while (!q.empty()) { Item item = q.front(); q.pop(); code = item.c; int d = item.d; int pos, x; decode(code, pos, x); if (x == DST) return d; swap0(pos, x, d); swap1(pos, x, d); up(pos, x, d); down(pos, x, d); left(pos, x, d); right(pos, x, d); } assert(0); return 0; }
inline bool BF(Graph &G, int Source, int Sink) { memset(dp, oo, sizeof(dp)); dp[Source] = 0; Q.push(Source); inQ[Source] = 1; while(!Q.empty()) { int Node = Q.front(); Q.pop(); inQ[Node] = 0; for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) { if(C[it->second] > 0 && dp[it->first] > dp[Node] + cost[it->second]) { dp[it->first] = dp[Node] + cost[it->second]; Father[it->first] = it->second; dad[it->first] = Node; if(inQ[it->first]) continue; Q.push(it->first); inQ[it->first] = 1; } } } return (dp[Sink] != oo); }
void bfsb() { pt now, next; while (!q.empty()) { now = q.front(); for (int i = 0; i < 4; i++) { int tx = now.x + dx[i]; int ty = now.y + dy[i]; if (vis[tx][ty] == 1) { cout << c[i]; vis[tx][ty] = 0; next.x = tx; next.y = ty; q.push(next); } } ans++; if (ans != n) cout << ",\n"; else cout << ".\n"; q.pop(); } }
void bfs() { int x, y; while (!Q.empty()) { xyxy t = Q.front(); Q.pop(); x = t.first.first; y = t.first.second; dist[x][y] = min(distance(t.first, t.second), dist[x][y]); if (x-1 >= 0 && distance(mk(x-1, y), t.second) < dist[x-1][y]) Q.push(mk(mk(x-1, y), t.second)); if (x+1 < n && distance(mk(x+1, y), t.second) < dist[x+1][y]) Q.push(mk(mk(x+1, y), t.second)); if (y-1 >= 0 && distance(mk(x, y-1), t.second) < dist[x][y-1]) Q.push(mk(mk(x, y-1), t.second)); if (y+1 < m && distance(mk(x, y+1), t.second) < dist[x][y+1]) Q.push(mk(mk(x, y+1), t.second)); } }
int main(){ ofstream fout ("sprime.out"); ifstream fin ("sprime.in"); fin>>n; if(n == 1){ fout<<2<<endl; fout<<3<<endl; fout<<5<<endl; fout<<7<<endl; } else{ q.push(2); q.push(3); q.push(5); q.push(7); findsp(n); } while(!q.empty()){ fout<<q.front()<<endl; q.pop(); } return 0; }
int bfs() { int x_var,y_var,x,y; int temp_x[]={-2,-2,-1,-1, 1, 1, 2, 2}; int temp_y[]={-1, 1, 2,-2, 2,-2, 1,-1}; while(!q.empty()) { node curr=q.front(); x_var=curr.x_pos; y_var=curr.y_pos; q.pop(); for(int i=0;i<8;i++) { x=temp_x[i]+x_var; y=temp_y[i]+y_var; if(x <0 || y <0 || x >9 || y >9 || matrix[x][y]==false) continue; matrix[x][y]=false; //cout<<"x="<<x<<"y="<<y<<"\n"; q.push(node(x,y)); } } }
int BFS() { while (!states.empty()) { state t = states.front(); states.pop(); if (t.cups[0] == T || t.cups[1] == T || t.cups[2] == T || t.cups[3] == T) return t.step; //fulfill && empty for (int i = 0; i<4; i++) { fulfill_empty(t, i, volume[i]); fulfill_empty(t, i, 0); } //pour to each other for (int i = 0; i<4; i++) { for (int j = 0; j<4; j++) { if (i != j) pourToEachOther(t, i, j); } } } return -1; }
void bfs(int src) { visited.clear(); queue<int>().swap(que); que.push(src); visited.insert(src); while(!que.empty()) { int curr = que.front(); que.pop(); printf("%d ", curr); for(auto&& next: g[curr]) { if(visited.count(next) <= 0) { que.push(next); visited.insert(next); } } } printf("\n"); }
void spfa(int x) { d[x]=0; q.push(x); while(!q.empty()) { int now=q.front(); q.pop(); for(int i=head[now];i!=-1;i=nex[i]) { int nexx=to[i]; if(d[nexx]==-1) { d[nexx]=d[now]+1; q.push(nexx); } } } }
void bfs(int sx, int sy, int dist[705][705]){ memset(dist,0x3f,705 * 705 * sizeof(int)); qx.push(sx); qy.push(sy); dist[sx][sy] = 0; while(!qx.empty()){ int xf = qx.front(); int yf = qy.front(); qx.pop(), qy.pop(); for(int i=0; i<4; i++){ if(xf + dx[i] < 0 || yf + dy[i] < 0 || xf + dx[i] >= n || yf + dy[i] >= m){ continue; } if(str[xf + dx[i]][yf + dy[i]] == 'I'){ continue; } if(dist[xf + dx[i]][yf + dy[i]] < 1e9) continue; qx.push(xf + dx[i]); qy.push(yf + dy[i]); dist[xf + dx[i]][yf + dy[i]] = dist[xf][yf] + 1; } } }
int bfs(int start, int count){ q.push(mp(start, count)); visited[start] = true; pair<int, int> p; while(!q.empty()){ p = q.front(); q.pop(); int floor = p.first; int c = p.second; if(floor == G){ return c; } if(!visited[floor+U] && floor+U <= F){ visited[floor+U] = true; q.push(mp(floor+U, c+1)); } if(!visited[floor-D] && floor-D >= 0){ visited[floor-D] = true; q.push(mp(floor-D, c+1)); } } return -1; }
string Calculation::GetAns(queue<string> wque){ while(!sd.empty()) sd.pop(); while(!sc.empty()) sc.pop(); string str; str=wque.front(); if(str[0]=='e') return str; stringstream sstr; double d; char c; char k; sc.push('('); while(!wque.empty()){ sstr.clear(); sstr.str(""); sstr<<wque.front(); str=sstr.str(); wque.pop(); if( str[0]<='9'&&str[0]>='0' ){ sstr>>d; sd.push(d); k=')'; }else{
void BFSfire(){ int r,c; while ( !q.empty() ){ tmp=q.front(); q.pop(); for (int i=0;i<4;++i){ r=tmp.r+dr[i]; c=tmp.c+dc[i]; if ( r>=0 && c>=0 && c<col && r<row && fire[r][c]==INF && mp[r][c]!='#'){ ff.r=r; ff.c=c; ff.val=tmp.val+1; fire[r][c]=ff.val; q.push(ff); } } } }
int getans(int start) { int ret=0; int u; que.push(start); inque[start]=1; while(!que.empty()) { u=que.front(); que.pop(); ++ret; in[u]=1; for(int i=0;i<N;++i) for(int j=0;j<N;++j) if(in[i] && in[j] && inque[map1[i*N+j]]==0) { que.push(map1[i*N+j]); inque[map1[i*N+j]]=1; } } return ret; }
bool spfa() { memset(dist,0x3f,sizeof(dist)); memset(inQ,0,sizeof(inQ)); dist[S]=0; Q.push(S); while(!Q.empty()) { int x=Q.front(); Q.pop(); inQ[x]=0; for(int i=head[x];i!=-1;i=edge[i].next) if(edge[i].flow>0 && dist[edge[i].to]>dist[x]+edge[i].cost) { pre[edge[i].to]=i; dist[edge[i].to]=dist[x]+edge[i].cost; if(!inQ[edge[i].to]) { inQ[edge[i].to]=1; Q.push(edge[i].to); } } } return dist[T]!=inf; }