MisakaAzusa

题目链接:https://www.luogu.org/problemnew/show/P1825
带有传送门的迷宫问题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2001;
char map[maxn][maxn];
int fx[4] = {0, 1, 0, -1};
int fy[4] = {1, 0, -1, 0};
int n, m, ex, ey, sx, sy;
struct map{
    int x, y, t;
}q[4000001];
struct door{
    int x1, y1, x2, y2;
}d[40];
void bfs()
{
    int head = 1, tail = 2;
    q[head].x = sx, q[head].y = sy, q[head].t = 0;
    while(head != tail)
    {
        for(int i = 0; i < 4; i++)
        {
            int nowx = q[head].x + fx[i];
            int nowy = q[head].y + fy[i];
            if(nowx == ex && nowy == ey)
            {
                printf("%d", q[head].t + 1);
                return ;
            }
            if(nowx > n || nowx <= 0 || nowy > m || nowy <= 0 || map[nowx][nowy] == \'#\') continue;
            if(map[nowx][nowy] >= \'A\' && map[nowx][nowy] <= \'Z\')
            {
                if(nowx == d[map[nowx][nowy]-\'A\'].x1 && nowy == d[map[nowx][nowy]-\'A\'].y1)
                {
                    q[tail].x = d[map[nowx][nowy]-\'A\'].x2;
                    q[tail].y = d[map[nowx][nowy]-\'A\'].y2;
                    q[tail].t = q[head].t + 1;
                    tail++;
                }
                if(nowx == d[map[nowx][nowy]-\'A\'].x2 && nowy == d[map[nowx][nowy]-\'A\'].y2)
                {
                    q[tail].x = d[map[nowx][nowy]-\'A\'].x1;
                    q[tail].y = d[map[nowx][nowy]-\'A\'].y1;
                    q[tail].t = q[head].t + 1;
                    tail++;
                }
            }
            else
            {
                map[nowx][nowy] = \'#\';
                q[tail].x = nowx;
                q[tail].y = nowy;
                q[tail].t = q[head].t + 1;
                tail++;
            }
        }
        head++;
    }
}
int main()
{
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            cin>>map[i][j];
            if(map[i][j] == \'@\') sx = i, sy = j;
            if(map[i][j] == \'=\') ex = i, ey = j;
            if(map[i][j] <= \'Z\' && map[i][j] >= \'A\')
            {
                if(d[map[i][j]-\'A\'].x1 == 0 && d[map[i][j]-\'A\'].y1 == 0)
                {d[map[i][j]-\'A\'].x1 = i, d[map[i][j]-\'A\'].y1 = j; continue;}
                if(d[map[i][j]-\'A\'].x2 == 0 && d[map[i][j]-\'A\'].y2 == 0)
                {d[map[i][j]-\'A\'].x2 = i, d[map[i][j]-\'A\'].y2 = j; continue;}
            }
        }
    bfs();
    return 0;
}

分类:

技术点:

相关文章:

  • 2019-08-31
  • 2018-07-19
  • 2018-12-14
  • 2021-11-23
  • 2021-12-04
  • 2018-07-25
  • 2019-09-25
  • 2021-08-11
猜你喜欢
  • 2021-11-13
  • 2021-11-13
  • 2021-11-13
  • 2021-11-13
  • 2017-11-22
  • 2018-02-25
  • 2021-05-30
相关资源
相似解决方案