题意:求a到r的最短路,,到#时的路的长度为2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,m;
const int N = 209;
char map[N][N];
bool visit[N][N];
struct node
{
int x,y,dis;
bool operator<(const node t) const
{
return dis>t.dis;
}
} st,en;
int dx[]= {0,0,-1,1};
int dy[]= {-1,1,0,0};
bool oor(int x,int y)
{
if(x<0||x>=n) return false;
if(y<0||y>=m) return false;
return true;
}
int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
scanf("%s",map[i]);
for(int j=0; j<m; j++)
if(map[i][j]=='a')
st.x=i,st.y=j;
else if(map[i][j]=='r')
en.x=i,en.y=j;
}
priority_queue<node> que;
st.dis =0;
while(!que.empty()) que.pop();
memset(visit,false,sizeof(visit));
que.push(st);
node e,t;
bool ou = false;
while(!que.empty())
{
e= que.top();
que.pop();
if(e.x==en.x&&e.y==en.y)
{
printf("%d\n",e.dis);
ou = true;
break;
}
if(visit[e.x][e.y]) continue;
visit[e.x][e.y] = true;
for(int i=0; i<4; i++)
{
int tx = e.x+dx[i],ty = e.y+dy[i];
if(!oor(tx,ty)||visit[tx][ty]||map[tx][ty]=='#') continue;
if(map[tx][ty]=='x') t.dis = e.dis+2;
else t.dis=e.dis+1;
t.x = tx;
t.y = ty;
que.push(t);
}
}
if(!ou)
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}