HRBUST1621 优先队列

注意优先队列重载符号的问题

都是小于的小的在前面,第二个大于则是大的在前面

不能重载大于号

并且每走一步要将改点锁定,即砌墙

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
int nextstep[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
char a[201][201];
struct formation
{
    int x;
    int y;
    int step;
    bool operator<(const formation b)const
    {
        return this->step >b.step;
    }
};
int main()
{
    int num,tx,ty,sx,sy,lx,ly,i,j;
    priority_queue<formation>q;
    while(cin>>num)
    {
        while(num--)
        {
            int minstep=999999;
            formation stu;
            int hang,lie;
            cin>>hang>>lie;
            memset(a,0,sizeof(a));
            for(i=1; i<=hang; i++)
                for(j=1; j<=lie; j++)
                {
                    cin>>a[i][j];
                    if(a[i][j]=='Z')sx=i,sy=j;
                    if(a[i][j]=='W')lx=i,ly=j;
                }
            stu.x=sx;
            stu.y=sy;
            stu.step=0;
            while(!q.empty())
            {
                q.pop();
            }
            q.push(stu);
            while(!q.empty())
            {
                for(i=0; i<=3; i++)
                {
                    tx=q.top().x+nextstep[i][0];
                    ty=q.top().y+nextstep[i][1];
                    if(tx>=1&&tx<=hang&&ty>=1&&ty<=lie&&a[tx][ty]!='#')
                    {
                        stu.x=tx;
                        stu.y=ty;
                        if(a[tx][ty]=='.'||a[tx][ty]=='W')
                            stu.step=q.top().step+1;
                        else
                            stu.step=q.top().step+(a[tx][ty]-'0')+1;
                        if(a[tx][ty]=='W')
                        {
                            if(minstep>stu.step)
                                minstep=stu.step;
                        }
                        a[tx][ty]='#';
                        q.push(stu);
                    }
                }
                q.pop();
            }
            if(minstep!=999999)cout<<minstep<<endl;
            else cout<<"IMPOSSIBLE"<<endl;
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41548233/article/details/79247574