求助,只有十分

P1649 [USACO07OCT] Obstacle Course S

MilkyCoffee @ 2021-01-09 14:59:06

如题,小测的时候没调出来的题求助。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int NR = 1e2 + 5;

struct node {
    int from, x, y, res;
};

int n, sx, sy, ex, ey, ans = 2e9;
char s[NR][NR];
bool vis[NR][NR];

int dx[10] = {0, 0, 1, -1};
int dy[10] = {1, -1, 0, 0};

queue <node> q;
void bfs() {
    memset(vis, 0, sizeof(vis));
    q.push((node){5, sx, sy, 0});
    vis[sx][sy] = 1;
    while (!q.empty()) {
        int from = q.front().from, x = q.front().x, y = q.front().y, res = q.front().res;
        q.pop();
        if (x == ex && y == ey) {
            ans = min(ans, res - 1);
        }
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny] && s[nx][ny] != 'x') {
                int tmp = 0;
                tmp = res;
                if (from != i) tmp++;
                q.push((node){i, nx, ny, tmp});
                vis[nx][ny] = 1;
            }
        }
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> s[i][j];
            if (s[i][j] == 'A') {
                sx = i;
                sy = j;
            } if (s[i][j] == 'B') {
                ex = i;
                ey = j;
            }
        }
    }

    bfs();
    cout << ans << endl;
    return 0;
}

by ieeqwq @ 2021-01-09 15:02:14

?WA 还是 TLE


by MilkyCoffee @ 2021-01-09 15:03:02

@我谔谔 WA


by ieeqwq @ 2021-01-09 15:21:58

感觉像是vis的问题


by ieeqwq @ 2021-01-09 15:22:06

不太清楚


by 梦游的小雪球 @ 2021-01-09 15:32:48

这就是您犇犇发I AK IOI的真实目的?


by 梦游的小雪球 @ 2021-01-09 15:32:59

这就是您犇犇发I AK IOI的真实目的?


|