HDU 1242

来源:互联网 发布:126邮箱imap端口 编辑:程序博客网 时间:2024/06/11 18:22

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32916    Accepted Submission(s): 11522


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.)
 

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.
 

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
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003 
 

POINT:
多起点的BFS。 遇X时间再多加1

#include <string>#include <string.h>#include <iostream>#include <queue>#include <math.h>#include <stdio.h>using namespace std;#define LL long longconst int maxn = 222+55;char mp[maxn][maxn];int flag[maxn][maxn];int n,m;int ai,aj;int dir[4][2]={0,1,0,-1,1,0,-1,0};struct node{int x,y,cnt;node(int xx,int yy,int cc):x(xx),y(yy),cnt(cc){}};int ans;void bfs(int xxx,int yyy){queue<node> q;q.push(node(xxx,yyy,0));while(!q.empty()){int x=q.front().x;int y=q.front().y;int cnt=q.front().cnt;if(mp[x][y]=='a'){ans=min(cnt,ans);return;}q.pop();for(int i=0;i<4;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(xx<1||xx>n||yy<1||yy>m||mp[xx][yy]=='#') continue;if(mp[xx][yy]=='x'){if(flag[xx][yy]!=0&&flag[xx][yy]<=cnt+2) continue;q.push(node(xx,yy,cnt+2));flag[xx][yy]=cnt+2;}else{if(flag[xx][yy]!=0&&flag[xx][yy]<=cnt+1) continue;q.push(node(xx,yy,cnt+1));flag[xx][yy]=cnt+1;}}}}int main(){while(~scanf("%d %d",&n,&m)){ans=0x3f3f3f3f;for(int i=1;i<=n;i++){scanf("%s",mp[i]+1);}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){flag[i][j]=0;if(mp[i][j]=='a'){ai=i;aj=j;}}}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(mp[i][j]=='r'){bfs(i,j);}}}if(ans==0x3f3f3f3f) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0;}


原创粉丝点击