1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream> #include <queue>
using namespace std;
const int N = 1010, M = N * N;
#define x first #define y second
#define PII pair<int, int>
int g[N][N]; bool st[N][N]; PII pre[N][N]; int n;
int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0};
int main() { cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> g[i][j]; } } queue<PII> q, tq; PII tep = {-1, -1}; fill(pre[0], pre[0] + n * n, tep); q.push({n - 1, n - 1}); bool flag = true;
while(!q.empty()) { auto t = q.front(); q.pop(); if (st[t.x][t.y]) continue; st[t.x][t.y] = true; for (int i = 0; i < 4; ++i) { int x = t.x + dx[i], y = t.y + dy[i]; if (x < 0 || y < 0 || x >= n || y >= n || g[x][y] || st[x][y]) continue; pre[x][y] = t; q.push({x, y}); } }
PII end = {0, 0}; cout << 0 << ' ' << 0 << endl;
while(end.x != n - 1 || end.y != n - 1) { printf("%d %d\n", pre[end.x][end.y].x, pre[end.x][end.y].y); int x = end.x, y = end.y; end.x = pre[x][y].x, end.y = pre[x][y].y; } }
|