Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First 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.
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.
Output
For 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需要多1s时间,如果走不到输出"Poor ANGEL has to stay in the prison all his life."。
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //#define LOCAL int n,m; char mp[210][210]; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int ex,ey; int vis[210][210]; struct Node { int x,y; int time; } cp; int bfs() { queue<Node> q; while(!q.empty())q.pop(); q.push(cp); while(!q.empty()) { cp=q.front(),q.pop(); if(cp.x==ex&&cp.y==ey)return cp.time; vis[cp.x][cp.y]=1; for(int i=0; i<4; i++) { Node np; np.x=cp.x+dir[i][0]; np.y=cp.y+dir[i][1]; np.time=cp.time+1; if(mp[np.x][np.y]=='#')continue; if(vis[np.x][np.y])continue; if(np.x<0||np.y<0||np.x>=n||np.y>=m)continue; if(mp[np.x][np.y]=='x')np.time++; q.push(np); } } return -1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL ios::sync_with_stdio(false); while(cin>>n>>m) { msp,msv; for(int i=0; i<n; i++)cin>>mp[i]; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mp[i][j]=='a')ex=i,ey=j; else if(mp[i][j]=='r')cp.x=i,cp.y=j; } cp.time=0; vis[cp.x][cp.y]=1; int ans=bfs(); if(ans==-1)printf("Poor ANGEL has to stay in the prison all his life. "); else printf("%d ",ans); } return 0; }
Answer for ZOJ
在ZOJ里再一次交了自己的代码,就发现问题了,竟然MLE了,HDU的数据也太弱了吧。
这次用优先队列去做。
这次用优先队列去做。
#include <cstdio> #include <iostream> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <vector> #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //map,size int n,m; char mp[210][210]; //visited bool vis[210][210]; //direction int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; struct Node { int x,y; int time; friend bool operator <(const Node &a,const Node &b) { return a.time>b.time; } } cp,np; //current point,new point int bfs() { //creat a priority queue priority_queue<Node> q; //clear while(!q.empty())q.pop(); msv; vis[cp.x][cp.y]=1; q.push(cp); while(!q.empty()) { cp=q.top(),q.pop(); //return answer if(mp[cp.x][cp.y]=='r') return cp.time; for(int i=0;i<4;i++) { //new point,four direction np.x=cp.x+dir[i][0]; np.y=cp.y+dir[i][1]; np.time=cp.time+1; //out of map if(np.x<0||np.x>=n||np.y<0||np.y>=m)continue; if(mp[np.x][np.y]=='#')continue; if(vis[np.x][np.y])continue; if(mp[np.x][np.y]=='x')np.time++; vis[np.x][np.y]=1; q.push(np); } } //can't find return -1; } int main() { while(cin>>n>>m) { for(int i=0; i<n; i++) cin>>mp[i]; //get start point for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mp[i][j]=='a') { cp.x=i,cp.y=j; cp.time=0; break; } } //bfs int ans=bfs(); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life. "); else printf("%d ",ans); } return 0; }