1 条题解

  • 0
    @ 2025-10-8 16:57:50
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 350;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    
    struct point { int x, y, t; };
    char a[N][N];
    bool vis[N][N];
    int n, m;
    
    void go(int &x, int &y) // 传送门的函数
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if ((a[i][j] == a[x][y]) && !((i == x) && (j == y)))
                {
                    x = i;
                    y = j;
                    return;
                }
    }
    int main()
    {
        scanf("%d%d", &n, &m);
        memset(vis, false, sizeof(vis));
        queue<point> Q;
        for (int i = 1; i <= n; i++)
        {
            char s[N]; scanf("%s", s + 1);
            for (int j = 1; j <= m; j++)
            {
                a[i][j] = s[j];
                if (a[i][j] == '@') Q.push(point{i, j, 0}); // 获取起点坐标
            }
        }
    
        while (!Q.empty())
        {
            point p = Q.front(); Q.pop();
            if (a[p.x][p.y] == '=') { printf("%d\n", p.t); return 0; }
            if (a[p.x][p.y] >= 'A' && a[p.x][p.y] <= 'Z') go(p.x, p.y);
    
            for (int i = 0; i <= 3; i++)
            {
                int x = p.x + dx[i];
                int y = p.y + dy[i];
                if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#' && !vis[x][y])
                {
                    vis[x][y] = true;
                    Q.push(point{x, y, p.t + 1});
                }
            }
        }
        return 0;
    }
    
    • 1

    *【宽搜】玉米迷宫[USACO11OPEN] Corn Maze S

    信息

    ID
    1547
    时间
    1000ms
    内存
    512MiB
    难度
    6
    标签
    递交数
    114
    已通过
    31
    上传者