10分 MLE 求条

P1746 离开中山路

yzx_yzx @ 2024-09-08 21:11:34

#include <bits/stdc++.h>
using namespace std;
long long n, x1, qy1, x2, y2;
char a[1050][1050];

struct nn {
    long long x, y, st;
};

void bfs() {
    queue<nn>p;
    p.push({x1, qy1, 0});
    while (!p.empty()) {
        nn now = p.front();
        long long x = now.x, y = now.y, st = now.st;
        p.pop();
        if (a[x][y] == '0') {
            if (x == x2 && y == y2) {
                cout << st;
                exit(0);
            }
            p.push({x + 1, y, st + 1});
            p.push({x - 1, y, st + 1});
            p.push({x, y + 1, st + 1});
            p.push({x, y - 1, st + 1});
        }

    }

}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }
    cin >> x1 >> qy1 >> x2 >> y2;
    if (a[x2][y2] == '1' || a[x1][qy1] == '1') {
        cout << '0';
        return 0;
    }
    bfs();
    return 0;
}

by 4041nofoundGeoge @ 2024-09-08 21:15:17

哥们void改到main函数试试 @yzx_yzx


by yzx_yzx @ 2024-09-08 21:19:09

@4041nofoundGeoge 不行,一样的


by 4041nofoundGeoge @ 2024-09-08 21:30:41

@yzx_yzx 不用long long再加上用v is数组记录当前有没有走过,dis记录路程长度估计就好了,longlong太长了


|