HDU----1242(BFS)
作者:互联网
题目链接
Rescue
代码如下
#include<bits/stdc++.h>
using namespace std;
const int M = 205;
int dx[] = {1,0,0,-1};
int dy[] = {0,1,-1,0};
int n,m;
int vis[M][M];char mp[M][M];
struct dot
{
int x,y,time;
};
bool in(dot gx)
{
if(gx.x>=0&&gx.x<n&&gx.y>=0&&gx.y<m) return true;
return false;
}
int main ()
{
while(~scanf("%d%d",&n,&m))
{
dot gx;queue<dot>q;
memset(vis,0,sizeof vis);
for(int i = 0;i<n;i++)
for(int j = 0;j<m;j++)
cin>>mp[i][j];
for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
{
if(mp[i][j]=='r')
{
gx.x = i;gx.y = j;gx.time = 0;
}
else if(mp[i][j]=='#') vis[i][j] = 1;
}
}
q.push(gx);vis[gx.x][gx.y] = 1;int step = 0;
while(!q.empty())
{
dot tmp,nex;
tmp = q.front(),q.pop();
if(mp[tmp.x][tmp.y]=='x') ++tmp.time;
for(int i = 0;i<4;i++)
{
nex.x = tmp.x+dx[i];
nex.y = tmp.y+dy[i];
nex.time = tmp.time+1;
if(in(nex)&&!vis[nex.x][nex.y])
{
q.push(nex);vis[nex.x][nex.y] = 1;
if(mp[nex.x][nex.y]=='a')
{
step = nex.time;break;
}
}
if(step>0) break;
}
}
if(step>0) printf("%d\n",step);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
标签:tmp,BFS,HDU,int,gx,----,vis,mp,nex 来源: https://blog.csdn.net/m0_53916335/article/details/119319819