ZOJ1649

来源:互联网 发布:中国十大网络主播 编辑:程序博客网 时间:2024/06/04 18:32

一个迷宫,许多个r,主人公在a,坏人在x,走到坏人的格子里需要打死坏人

我能r们最少多少时间找到a

题解:倒过来a分身找r就好,判重用到x,y的最短时间

#include<bits/stdc++.h>#define X first#define Y secondusing 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;}


0 0
原创粉丝点击