题目意思: 起点是a,终点是r,#不可走,x可走但是要多花费一单位时间,问你从a到r的最短时间是多少。
思路:优先队列板子(超级简单的好吧),就是使用优先队列,然后每次拿出时间一致的进行搜索,如果找到该点就直接输出当前的时间即可。
注意优先队列的一个排序函数,因为我优先队列里面用的是结构体,所以要写个自定义函数用来判断排序条件:
bool operator<(lk a,lk b)
{
return a.cnt > b.cnt;
}
其他的也没什么好讲的,就是要注意如果到不了的话的输出后面是带了逗号的(麻麻的wa了) 。
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
// ll s[N];
char ant[220][220];
int dx[5]={0,0,1,0,-1};
int dy[5]={0,1,0,-1,0};
bool st[220][220];
struct lk{
int x;
int y;
int cnt;
};
bool operator<(lk a,lk b)
{
return a.cnt > b.cnt;
}
void bfs(int x,int y)
{
priority_queue<lk>q;
st[x][y]=1;
q.push({x,y,0});
while(q.size())
{
lk k=q.top();
q.pop();
if(ant[k.x][k.y]=='a'){
cout<<k.cnt<<endl;
return ;
}
for(int i =1;i<=4;i++)
{
int x=k.x+dx[i],y=k.y+dy[i];
if(x<1||x>n||y<1||y>m)continue;
if(st[x][y]||ant[x][y]=='#')continue;
st[x][y]=1;
if(ant[x][y]=='x')
q.push({x,y,k.cnt+2});
else
q.push({x,y,k.cnt+1});
}
}
cout<<"Poor ANGEL has to stay in the prison all his life.\n";
return ;
}
int main()
{
int t;
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
memset(st,0,sizeof st);
int xx,yy;
for(int i =1;i<=n;i++)
{
cin>>ant[i]+1;
for(int j =1;j<=m;j++)
if(ant[i][j]=='r'){
xx=i,yy=j;
}
}
bfs(xx,yy);
}
return 0;
}