http://acm.hdu.edu.cn/showproblem.php?pid=1242

#include<stdio.h>
#include
<string.h>
#include
<iostream>
#include
<queue>
using namespace std;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int si,sj;//公主的起始横纵坐标,
int n,m;
char map[201][201];
struct node
{
int x,y;
int time;
};
priority_queue
<node> q;
bool operator<(node a,node b)
{
return a.time>b.time;
}
int out_line(int a,int b)
{
if(a >= 0 && a< n && b>=0 && b < n)return 1;
else return 0;
}
void getmap()
{
int i,j;
memset(map,
0,sizeof(map));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
scanf(
"%1s",&map[i][j]);
if(map[i][j]=='a') si=i,sj=j;
}
}
int bfs(int si,int sj)
{
int i,mark[201][201];
node cur,next;
while(!q.empty()) //清队列
q.pop();
memset(mark,
0,sizeof(mark));
cur.x
=si, cur.y=sj, cur.time=0, mark[cur.x][cur.y]=1;
q.push(cur);
while(!q.empty())
{
cur
=q.top();
q.pop();
for(i=0;i<4;i++)
{
next.x
=cur.x+dir[i][0];
next.y
=cur.y+dir[i][1];
if(!mark[next.x][next.y] && out_line(next.x,next.y) &&
map[next.x][next.y]
!='#')
{
mark[next.x][next.y]
=1;
if(map[next.x][next.y]=='x')next.time=cur.time+2;
else next.time=cur.time+1;
if(map[next.x][next.y]=='r') return next.time;
q.push(next);
}
}
}
return -1;
}
int main()
{
int ans;
while(~scanf("%d%d",&n,&m))
{
getmap();
ans
=bfs(si,sj);
if(ans==-1)
printf(
"Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
}
return 0;
}

posted on 2011-05-17 15:04  敌敌  阅读(546)  评论(0编辑  收藏  举报