Applese Maze (PQ + bfs)

Copyright: private individuals do question summed up ~ https://blog.csdn.net/tb_youth/article/details/90212861

Links: https://ac.nowcoder.com/acm/problem/22344
Source: Cattle-off network

Proficient in programming Applese wrote a double game.

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

In the maze, there are some squares are the pool, in the water only when Applese property can pass; there are some squares magma, it is time to fire only when Applese properties may be; there are some squares are walls, anyway We can not pass; others are lattice space (including start and end), can pass through freely.

There are some open space in mysterious props can make Applese convert their property (from property into water or fire property from fire attribute becomes water property, requires a unit of time).

Known Applese can walk within a unit cell towards a four directions, and begin attribute in water, rear props open space where the pickup is only available immediately (or not used), and can be used multiple times. Seeking it out of the maze minimum time required.
Input Description:
The first line two positive integers n, m represents the size of the maze.
Next n lines, each line a string of length m. Description map.
Wherein 'S' represents the starting point, 'T' indicates the end, '' represents a space, 'w' represents a magma, '~' represents the pool, '@' represents prop, '#' represents disorder.
Ensure that the map is only a start and end points, props are located in the open space.
Here Insert Picture Description

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

Output:

18

Note:
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;
}

Guess you like

Origin blog.csdn.net/tb_youth/article/details/90212861
Recommended