[C艹] bfs WA50'求助
查看原帖
[C艹] bfs WA50'求助
557826
D_guard楼主2022/8/16 17:30

题目:P2130 狂奔的Wzf

评测记录见此

蒟蒻代码:

#include <cstdio>
#include <queue>
#include <iostream>

using namespace std;
int n, m, x, y, t,
    vst[1000][1000],
    h[2][1000][1000],
    dic[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512},
    trn[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
char tmp;

struct dot
{
    int x, y, t;
} nd, ed;

queue<dot> q;

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            cin >> tmp;
            if (tmp == 'X')
            {
                h[0][i][j] = h[0][i][j - 1] + 1;
                h[1][i][j] = h[1][i - 1][j] + 1;
            }
            else
            {
                if (tmp == '#')
                    ed.x = i, ed.y = j;
                h[0][i][j] = h[0][i][j - 1];
                h[1][i][j] = h[1][i - 1][j];
            }
        }
    nd.x = nd.y = nd.t = 0;
    q.push(nd);
    while (!q.empty())
    {
        x = q.front().x;
        y = q.front().y;
        t = q.front().t;
        if (x == ed.x && y == ed.y)
        {
            cout << t << '\n';
            return 0;
        }
        q.pop();
        if (vst[x][y])
            continue;
        vst[x][y] = 1;
        nd.t = t + 1;
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 10; ++j)
            {
                nd.x = x + trn[i][0] * dic[j];
                nd.y = y + trn[i][1] * dic[j];
                if (nd.x < 0 || nd.y < 0 || nd.x >= n || nd.y >= m)
                    break;
                if (h[i % 2][nd.x][nd.y] - h[i % 2][x][y])
                    break;
                q.push(nd);
            }
    }
    cout << -1 << '\n';
    return 0;
}

2022/8/16 17:30
加载中...