7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13
#include <stdio.h>
#include <algorithm>
using namespace std;
#include <string.h>
#include <queue>
#include <stack>
#include <math.h>
char map[202][202];
int vis[202][202];
int n,m,x2,y2;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y;
int step;
bool friend operator<(node a,node b)
{
return a.step>b.step;
}
}
;
bool check(int x,int y)
{
if(x<0 ||x>=n||y<0||y>=m ||vis[x][y]||map[x][y]=='#')
return 0;
return 1;
}
int bfs(int x,int y)
{
int i;
priority_queue<node>q;
node st,ed;
st.x=x;
st.y=y;
st.step=0;
q.push(st);
while(!q.empty())
{
st=q.top();
q.pop();
if(st.x==x2&&st.y==y2)
return st.step;
for(i=0;i<4;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(!check(ed.x,ed.y))
continue;
if(map[ed.x][ed.y]!='x')
ed.step=st.step+1;
else
ed.step=st.step+2;
vis[ed.x][ed.y]=1;
q.push(ed);
}
}
return 0;
}
int main()
{
int i,j,x1,y1;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='a')
{
x1=i;
y1=j;
}
else if(map[i][j]=='r')
{
x2=i;
y2=j;
}
}
vis[0][0]=1;
int ans=bfs(x1,y1);
if(ans)
printf("%d\n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}原文:http://blog.csdn.net/sky_miange/article/details/43827569