Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20938 Accepted Submission(s): 7486
was caught by the MOLIGPY! He was put in * by Moligpy. The *
is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs,
and GUARDs in the *.
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.)
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.
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 * all his
life."
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 200+10
int n,m,flag;
struct Node
{
int x,y,cost;
bool operator < (const Node &a)const//定义时间花费少的优先级高。
{
return cost>a.cost;
}
};
Node st,et,f,k;
int dx[]={,,-,};
int dy[]={,-,,};
int vis[maxn][maxn];
char map[maxn][maxn];
void bfs()
{
priority_queue<Node>q;
q.push(st);
while(!q.empty())
{
f=q.top();
for(int i=;i<;i++)
{
k.x=f.x+dx[i];
k.y=f.y+dy[i];
if(!vis[k.x][k.y]&&k.x>=&&k.x<n&&k.y>=&&k.y<m)
{
vis[k.x][k.y]=;
k.cost=f.cost+;
if(map[k.x][k.y]=='x')
k.cost++;
if(k.x==et.x&&k.y==et.y)
{
printf("%d\n",k.cost);
return;
}
q.push(k);
}
}
q.pop();
}
printf("Poor ANGEL has to stay in the * all his life.\n");
} int main()
{
while(~scanf("%d%d",&n,&m))
{
if(m==)
break;
memset(vis,,sizeof(vis));
getchar();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='#')
{
vis[i][j]=;
}
if(map[i][j]=='r')
{
st.x=i;
st.y=j;
st.cost=;
}
if(map[i][j]=='a')
{
et.x=i;
et.y=j;
} }
getchar();
}
bfs();
} return ;
}