Applese 走迷宫(优先队列+bfs)

版权声明:个人做题总结专用~ https://blog.csdn.net/tb_youth/article/details/90212861

链接:https://ac.nowcoder.com/acm/problem/22344
来源:牛客网

精通程序设计的 Applese 双写了一个游戏。

在这个游戏中,它被困在了一个 n×m 的迷宫中,它想要逃出这个迷宫。

在迷宫中,有一些方格是水池,只有当 Applese 处于水属性的时候才可以通过;有一些方格是岩浆,只有当 Applese 是火属性的时候可以通过;有一些方格是墙壁,无论如何都无法通过;另一些格子是空地(包括起点和终点),可以自由通过。

在一些空地上有神秘道具可以让 Applese 转换自己的属性(从水属性变为火属性或从火属性变为水属性,需要一个单位的时间)。

已知 Applese 在一个单位的时间内可以朝四个方向行走一格,且开始处于水属性,位于空地的道具拾取后只能在该处立即使用(或者不使用),且可以多次使用。求它走出迷宫需要的最少时间。
输入描述:
第一行两个正整数 n, m 表示迷宫的大小。
接下来 n 行,每行长度为 m 的字符串。描述地图。
其中 ‘S’ 表示起点,‘T’ 表示终点,’.’ 表示空地,‘w’表示岩浆,’~‘表示水池,’@’ 表示道具,’#'表示障碍。
保证地图中的起点和终点只有一个,道具都位于空地。
在这里插入图片描述

5 5
.w@..
.S#..
~w#..
.w..~
@w.~T

输出:

18

备注:
1≤n,m≤100
Ac_code:

#include <stdio.h>
#include <queue>
const int maxn = 105;
using namespace std;
int n,m,sx,sy,ex,ey;
struct point
{
    int x,y;
    int now, step;
    bool operator <(const point &p)const
    {
        return step > p.step;
    }
};
char mp[maxn][maxn];
bool vis[maxn][maxn][2];
int dx[]= {-1,1,0,0},dy[]= {0,0,-1,1};
int bfs()
{
    point s;
    s.x = sx,s.y = sy;
    s.now = 0,s.step = 0;
    vis[s.x][s.y][s.now] = true;
    priority_queue<point>q;
    q.push(s);
    while(!q.empty())
    {
        point k = q.top();
        q.pop();
        if(k.x==ex&&k.y==ey)
        {
            return k.step;
        }
        point t;
        for(int i = 0; i < 4; i++)
        {
            t.x = k.x + dx[i];
            t.y = k.y + dy[i];
            t.now = k.now;
            t.step = k.step + 1;
            if(t.x<0||t.x>=n||t.y<0||t.y>=m)
                continue;
            if(mp[t.x][t.y]=='#')
                continue;
            if(!vis[t.x][t.y][t.now])
            {
                if(mp[t.x][t.y]=='.'||mp[t.x][t.y]=='S'||mp[t.x][t.y]=='T'||mp[t.x][t.y]=='@')
                {
                    vis[t.x][t.y][t.now] = true;
                    q.push(t);
                }
                else if((mp[t.x][t.y]=='~'&&k.now==0)||(mp[t.x][t.y]=='w'&&k.now==1))
                {
                    vis[t.x][t.y][t.now] = true;
                    q.push(t);
                }
            }
            if(mp[k.x][k.y]=='@')
            {
                t.now = !k.now;
                if(vis[t.x][t.y][t.now])
                    continue;
                if((mp[t.x][t.y]=='~'&&t.now==1)||(mp[t.x][t.y]=='w'&&t.now==0))//这里要注意
                    continue;
                t.step = k.step+2;
                vis[t.x][t.y][t.now] = true;
                q.push(t);
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
    {
        scanf("%s",mp[i]);
        for(int j = 0; j  < m; j++)
        {
            if(mp[i][j]=='S')
            {
                sx = i;
                sy = j;
            }
            else if(mp[i][j]=='T')
            {
                ex = i;
                ey = j;
            }
        }
    }
    printf("%d\n",bfs());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tb_youth/article/details/90212861