Applese maze (priority queue + bfs)

Keywords: Attribute Programming

Links: https://ac.nowcoder.com/acm/problem/22344
Source: niuke.com

Adept at programming, apple ese double wrote a game.

In this game, it is trapped in a n × m maze, and it wants to escape from the maze.

In the maze, some squares are pools, which can only pass when Applese is in water property; some squares are magma, which can only pass when Applese is in fire property; some squares are walls, which can't pass in any case; others are open spaces (including start and end points), which can pass freely.

In some open spaces, there are mysterious props that enable Apple ese to change its attributes (from water attribute to fire attribute or from fire attribute to water attribute, it takes a unit of time).

It is known that Applese can walk in four directions in a unit of time, and it starts to be in the water property. After picking up the props in the open space, they can only be used (or not) immediately, and can be used multiple times. Find the minimum time it takes to get out of the maze.
Enter a description:
In the first line, two positive integers n and M represent the size of the maze.
Next n lines, each of which is a string of m length. Describe the map.
Where 'S' is the starting point,' T 'is the end point,' w 'is the open space,' w 'is the magma,' ~ 'is the pool,' @ 'is the prop,' Chen 'is the obstacle.
Make sure that there is only one starting and ending point in the map, and that the props are all in the open space.

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

Output:

18

Remarks:
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))//Pay attention here
                    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;
}

Posted by aperantos on Sun, 10 Nov 2019 13:06:25 -0800