标签:hdu1242
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
typedef struct node
{
int x,y,time;
friend bool operator <(node a,node b)
{
return b.time<a.time;
}
}Node;
char map[205][205];
int n,m,a,b,Time,d[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
void BFS()
{
priority_queue<Node> que;
Node p,q;
int i,x,y;
q.x=a;q.y=b;q.time=0;
que.push(q);
while(!que.empty())
{
q=que.top();
que.pop();
for(i=0;i<4;i++)
{
x=q.x+d[i][0];
y=q.y+d[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#')
{
if(map[x][y]=='a')
{
Time=q.time+1;
return ;
}
p.x=x;
p.y=y;
p.time=q.time+1;
if(map[x][y]=='x')
p.time=p.time+1;
map[x][y]='#';
que.push(p);
}
}
}
}
int main()
{
int i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(map[i][j]=='r')
{
a=i;
b=j;
}
Time=-1;
BFS();
if(Time!=-1)
printf("%d\n",Time);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:hdu1242
原文地址:http://blog.csdn.net/u011479875/article/details/47364323