题解:搜图的题,有权最短路用优先队列+bfs

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

struct node
{
    int x,y,step;
    friend bool operator < (node a,node b)
    {
        return a.step>b.step;
    }
};

char map[30][30];
int n,m,book[30][30],nex[4][2]={0,1,1,0,0,-1,-1,0},a,b,c;
priority_queue<node> Q;

bool judge(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return true;
    return false;
}

int bfs(int x,int y,int nx,int ny)
{
	if(x==nx&&y==ny)
		return 0;
    node xia,now;
    int i;
    while(!Q.empty())
        Q.pop();
    book[x][y]=1;
    now.x=x;
    now.y=y;
    now.step=0;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.top();
        Q.pop();
        for(i=0;i<4;i++)
        {
            xia.x=now.x+nex[i][0];
            xia.y=now.y+nex[i][1];
            if(judge(xia.x,xia.y) || book[xia.x][xia.y] || map[xia.x][xia.y]=='@')
                continue;
            if(map[xia.x][xia.y]=='T')
                xia.step=now.step+c;
            else if(map[xia.x][xia.y]=='.')
                xia.step=now.step+b;
            else
                xia.step=now.step+a;
            if(xia.x==nx && xia.y==ny)
                return xia.step;
            book[xia.x][xia.y]=1;
            Q.push(xia);
        }
    }
    return -1;
}

int main()
{
    int i,j,x,y,nx,ny,t=1;
    while(cin>>n>>m)
    {
    	cin>>a>>b>>c;
        getchar();
        memset(book,0,sizeof(book));
        for(i=0;i<n;i++)
            cin>>map[i];
        cin>>x>>y>>nx>>ny;
        cout<<"Case "<<t++<<": "<<bfs(x,y,nx,ny)<<endl;
    }
    return 0;
}