一个迷宫,许多个r,主人公在a,坏人在x,走到坏人的格子里需要打死坏人
我能r们最少多少时间找到a
题解:倒过来a分身找r就好,判重用到x,y的最短时间
#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
int n,m;
typedef pair<int,int> pii;
typedef pair<pii,int> piii;
const int movx[4]={0,1,0,-1};
const int movy[4]={1,0,-1,0};
void Gao()
{
char a[300][300];
int f[300][300];
for (int i=0;i<n;i++)
scanf("%s",a[i]);
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
f[i][j]=3276700;
int stx,sty;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
if (a[i][j]=='a')
stx=i,sty=j;
f[stx][sty]=0;
queue<piii> q;
q.push(make_pair(make_pair(stx,sty),0));
int ans=3276700;
while(!q.empty())
{
int tmx=q.front().X.X,tmy=q.front().X.Y,tmstp=q.front().Y;
q.pop();
if (a[tmx][tmy]=='r')
{
ans=min(ans,tmstp);
continue;
}
for (int i=0;i<4;i++)
{
int xx=tmx+movx[i];
int yy=tmy+movy[i];
if (xx>=n ||xx<0||yy>=m||yy<0)continue;
if (a[xx][yy]=='#') continue;
int pp=tmstp+(a[xx][yy]=='x'?2:1);
if (pp<f[xx][yy])
{
q.push(make_pair(make_pair(xx,yy),pp));
f[xx][yy]=pp;
}
}
}
if (ans==3276700)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<ans<<endl;
}
int main()
{
while (cin>>n>>m)
Gao();
return 0;
}
最后
以上就是懦弱巨人最近收集整理的关于ZOJ1649的全部内容,更多相关ZOJ1649内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复