标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24608 Accepted Submission(s): 8686
‘x‘进队两次,第一次进队标记为‘o‘,‘o‘出队的时候标记为‘.‘,然后‘.‘再次进队,之后的处理和其他点一样
因为要保证bfs同层的t相同,这样a第一次找到‘r‘就能保证时间最短
好像‘r‘好像可以有多个,所以本题最好‘a‘去找‘r‘
以前在zoj做过这题,还是缠了好久。。。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <string> const int inf =(1<<31)-1; const int MAXN = 2e2+10; using namespace std; struct step{ int x; int y; int t; }; int mov[4][2]={1,0,-1,0,0,1,0,-1}; queue<step>q; char G[MAXN][MAXN]; int n,m; int check(int x,int y){ if(x<0||y<0||x>=n||y>=m)return 0; if(G[x][y]==‘#‘)return 0; else return 1; } int main() { int sx,sy; int mmint; while(~scanf("%d%d",&n,&m)){ mmint = -1; for(int i=0;i<n;i++){ scanf("%s",G[i]); for(int j=0;j<m;j++){ if(G[i][j]==‘a‘){ sx = i; sy = j; } } } step t,fro; t.x=sx; t.y = sy; t.t = 0; q.push(t); G[sx][sy]=‘.‘; int nx,ny; while(!q.empty()){ fro = q.front(); q.pop(); if(G[fro.x][fro.y]==‘r‘){ mmint = fro.t; break; } if(G[fro.x][fro.y]==‘o‘){ G[fro.x][fro.y] = ‘.‘; fro.t = fro.t+1; q.push(fro); // cout<<"debug"<<endl; continue; } for(int i=0;i<4;i++){ nx = fro.x+mov[i][0]; ny= fro.y+mov[i][1]; if(check(nx,ny)){ if(G[nx][ny]==‘x‘){ G[nx][ny]=‘o‘; //cout<<"start"<<endl; }else if(G[nx][ny]==‘o‘){ continue; }else if(G[nx][ny]!=‘r‘){ G[nx][ny]=‘#‘; } //G[nx][ny]=‘#‘; t.x = nx; t.y = ny; t.t = fro.t+1; q.push(t); } } } while(!q.empty()){ q.pop(); } if(mmint==-1){ cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; }else{ cout<<mmint<<endl; } } //cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5434764.html