luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题解

Posted misakaazusa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题解相关的知识,希望对你有一定的参考价值。

题目链接: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;
}

以上是关于luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题解的主要内容,如果未能解决你的问题,请参考以下文章

luogu3146 [USACO16OPEN]248

luogu P2627 [USACO11OPEN]Mowing the Lawn G 单调队列优化dp

luogu P3147 [USACO16OPEN]262144

luogu P2949 [USACO09OPEN]工作调度Work Scheduling

[luogu3067 USACO12OPEN] 平衡的奶牛群

Luogu [USACO08OPEN]寻宝之路Clear And Present Danger