Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 13
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.
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
刚开始一直wa。。。就是不知道为什么,百度后才知道r有多个。。。a只有一个。然后我就把r和a的顺序换了一下~~~
AC~~~
![偷笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/titter.gif)
AC-code:
#include<stdio.h>
#include<cstring>
#include<queue>
#define INF 0xfffffff
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int m,n,mark[205][205],ans;
char a[205][205];
using namespace std;
struct node
{
int x,y,step;
}map,temp;
int jud(struct node p)
{
if(p.x<0||p.y<0||p.x>n||p.y>m)
return 0;
if(map.step>=ans)
return 0;
if(mark[p.x][p.y]||a[p.x][p.y]=='#')
return 0;
return 1;
}
int bfs(int c,int b)
{
map.x=c;
map.y=b;
map.step=0;
queue<node>q;
q.push(map);
while(!q.empty())
{
map=q.front();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=map.x+dx[i];
temp.y=map.y+dy[i];
temp.step=map.step+1;
if(jud(temp))
{
if(a[temp.x][temp.y]=='x')
temp.step++;
if(a[temp.x][temp.y]=='r')
{
if(ans>temp.step)
ans=temp.step;
continue;
}
mark[temp.x][temp.y]=1;
q.push(temp);
}
}
}
return ans;
}
int main()
{
int i,j,c,d;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=INF;
memset(mark,0,sizeof(mark));
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='a')
{
mark[i][j]=1;
c=i;d=j;
}
}
}
ans=bfs(c,d);
if(ans==INF)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return 0;
}