#include<iostream>
using namespace std;
#include<queue>
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char maze[201][201];
int n,m;
struct Node
{
int x,y,t;
Node(int xx,int yy,int tt);
bool operator < (Node b) const
{
return b.t<t;
}
};
Node::Node(int xx,int yy,int tt)
{
x=xx;
y=yy;
t=tt;
}
void bfs(int sx,int sy)
{
priority_queue<Node> pq;
Node temp(sx,sy,0);
pq.push(temp);
while(pq.size())
{
Node top=pq.top();
pq.pop();
maze[top.x][top.y]='#';
for(int i=0;i<4;i++)
{
temp.x=top.x+dir[i][0];
temp.y=top.y+dir[i][1];
if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)
{
continue;
}
if(maze[temp.x][temp.y]=='#')
{
continue;
}
if(maze[temp.x][temp.y]=='r')
{
cout<<top.t+1<<endl;
return;
}
if(maze[temp.x][temp.y]=='.')
{
temp.t=top.t+1;
}
if(maze[temp.x][temp.y]=='x')
{
temp.t=top.t+2;
}
pq.push(temp);
}
}
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main(void)
{
while(~scanf("%d %d",&n,&m))
{
int sx,sy;
for(int i=0;i<n;i++)
{
for(int k=0;k<m;k++)
{
cin>>maze[i][k];
if(maze[i][k]=='a')
{
sx=i;
sy=k;
}
}
}
bfs(sx,sy);
}
return 0;
}
【HDOJ 1242 Rescue】bfs+优先队列
最新推荐文章于 2020-04-08 15:46:34 发布