hdu 1242 Rescue

找优先队列的题目偶尔遇到的。。
真是坑爹啊。。 这道题的数据似乎存在其他符号orz。。
看了一下discuss 果然。。。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct sb
{
    int x;
    int y;
    int step;
    char me;
    bool ok;
    bool operator <(const sb&wakaka)const
    {
        return step>wakaka.step;
    }
}a,r,way[210][210];
priority_queue<sb>q;
int n,m;
int bfs()
{
    while(!q.empty())
    {
        q.pop();
    }
    q.push(a);
    way[a.x][a.y].ok=true;
    way[a.x][a.y].step=0;
    while(!q.empty())
    {
        int oldx=q.top().x;
        int oldy=q.top().y;
        int oldstep=q.top().step;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int newx=oldx+mov[i][0];
            int newy=oldy+mov[i][1];
            if(newx<0||newy<0||newy>=m||newx>=n||way[newx][newy].ok||!(way[newx][newy].me=='.'||way[newx][newy].me=='x'||way[newx][newy].me=='r'||way[newx][newy].me=='a'))
            {
                continue;
            }
            if(way[newx][newy].me=='x')
            {
                way[newx][newy].step=oldstep+2;
            }
            else
            {
                way[newx][newy].step=oldstep+1;
            }
            if(way[newx][newy].me=='r')
                return way[newx][newy].step;
            way[newx][newy].ok=true;
            q.push(way[newx][newy]);
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        char test;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>way[i][j].me;
                way[i][j].x=i;
                way[i][j].y=j;
                way[i][j].step=-1;
                way[i][j].ok=false;
                if(way[i][j].me=='a')
                {
                    a.x=i;
                    a.y=j;
                }
                else if(way[i][j].me=='r')
                {
                    r.x=i;
                    r.y=j;
                }
            }
        }
        int ans=bfs();
        if(ans==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(优先队列,HDU,bfs)