标签:algo pac kill sed ram scribe style nes break
InputFirst line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel‘s friend.
Process to the end of the file.
OutputFor each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
Sample Output
13
/*救人, 你在a,要救的人在r, ‘.‘是路可以走,‘#‘是墙,‘x‘是警卫,走路花费一秒,遇见警卫花费两秒。 问最小多少秒能救人, 能的话输出步数,不能的话输出"Poor ANGEL has to stay in the prison all his life."*/ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define ll long long #define inf 0x3fffffff using namespace std; const int maxn = 205; int n,m,xxx,yyy; char a[maxn][maxn]; int vis[maxn][maxn]; int dir[][2]={ {1,0},{0,1},{-1,0},{0,-1} }; struct Node { int xx,yy,time; //横纵坐标+时间 friend bool operator < (Node a,Node b) { return a.time > b.time; } }; int check(int x,int y) { if(x<0||x>=n||y<0||y>=m||a[x][y]==‘#‘||vis[x][y]) return 0; return 1; } /*int check(int x,int y) { if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == ‘#‘) return 1; return 0; }*/ int bfs(int x,int y) { memset(vis,0,sizeof(vis));//清空标记数组放到bfs里面!!!!! priority_queue<Node> q; while(!q.empty()) q.pop(); q.push(Node{x,y,0}); vis[x][y]=1; while(!q.empty()) { Node u=q.top(); q.pop(); Node tmp; if(a[u.xx][u.yy]==‘r‘) return u.time; for(int i=0;i<4;i++) { tmp.xx=u.xx+dir[i][0]; tmp.yy=u.yy+dir[i][1]; if( check(tmp.xx,tmp.yy) ) { vis[tmp.xx][tmp.yy]=1; if(a[tmp.xx][tmp.yy]==‘x‘) tmp.time=u.time+2; else tmp.time=u.time+1; q.push(tmp); } } } return -1; } int main() { memset(vis,0,sizeof(vis)); while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%s",&a[i]); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]==‘a‘) { xxx=i; yyy=j; break; } } } int ans=bfs(xxx,yyy); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } }
标签:algo pac kill sed ram scribe style nes break
原文地址:http://www.cnblogs.com/Roni-i/p/7407102.html