Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26496 Accepted Submission(s): 9380
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题解:天使被抓进监狱,ta的朋友们(r)要去解救ta,(注意:是朋友们~~~)每个人可以走的方向有上、下、左、右四种,有些地方可能还会有守卫(x),杀死一个守卫需要一单位的时间,每到达一个新地点也需要一单位的时间,求要救出天使需要的最少时间。 (由于朋友可能不只一个,所以我们可以假设让天使去找ta的朋友们,只要找到一个朋友,即被解救!) ---BFS---<pre name="code" class="cpp"><span style="font-family:SimSun;font-size:18px;"><span style="font-size:18px;"></span>
<span style="font-family:SimSun;font-size:18px;">#include<stdio.h> #include<queue> #include<functional> #include<string.h> using namespace std; int n,m,sx,sy,ex,ey; int vis[210][210]; char s[210][210]; int dx[4]={1,-1,0,0}; //表示四个方向 int dy[4]={0,0,1,-1}; int go(int x,int y) { if(0<=x&&x<n&&0<=y&&y<m&&s[x][y]!='#') return 1; return 0; } struct node { int x,y,step; friend bool operator < (node a,node b) { return a.step>b.step; } }; int bfs(int x,int y) { priority_queue<node>que; //对时间进行排序,时间短的优先级高 node p; p.x=x; p.y=y; p.step=0; que.push(p); while(!que.empty()) { node q1; p=que.top(); que.pop(); if(s[p.x][p.y]=='r') //找到朋友 return p.step; for(int i=0;i<4;i++) { q1.x=p.x+dx[i]; q1.y=p.y+dy[i]; if(go(q1.x,q1.y)&&!vis[q1.x][q1.y]) { vis[q1.x][q1.y]=1; //对走过的路进行标记,标记后则不再记录 if(s[q1.x][q1.y]=='x') q1.step=p.step+2; //到达此地但有守卫,花费两单位的时间 else q1.step=p.step+1; //无守卫 que.push(q1); } } } return 0; } int main() { while(~scanf("%d%d",&n,&m)) { memset(s,0,sizeof(s)); memset(vis,0,sizeof(vis)); getchar(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&s[i][j]); if(s[i][j]=='a') { sx=i;sy=j; } } getchar(); } vis[sx][sy]=1; int ans=bfs(sx,sy); if(!ans) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0; }</span> </span>