結果
問題 |
No.340 雪の足跡
|
ユーザー |
|
提出日時 | 2016-11-21 04:30:30 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,201 bytes |
コンパイル時間 | 1,614 ms |
コンパイル使用メモリ | 174,872 KB |
実行使用メモリ | 11,392 KB |
最終ジャッジ日時 | 2024-11-27 09:25:04 |
合計ジャッジ時間 | 4,058 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 30 WA * 2 |
ソースコード
#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #define getchar getchar_unlocked #define putchar putchar_unlocked #endif int in() { int n, c; while ((c = getchar()) < '0') if (c == EOF) abort(); n = c - '0'; while ((c = getchar()) >= '0') n = n * 10 + c - '0'; return n; } short hor[1001][1001]; short ver[1001][1001]; int dist[1001][1001]; int main() { int w = in(), h = in(), n = in(); for (int i = 0; i < n; i++) { int m = in(); int pre = -1; for (int i = 0; i <= m; i++) { int cur = in(); if (pre != -1) { int y = pre / w; int x = pre % w; int yy = cur / w; int xx = cur % w; if (y == yy) { hor[y][min(x, xx)]++; hor[y][max(x, xx)]--; } else { ver[x][min(y, yy)]++; ver[x][max(y, yy)]--; } } pre = cur; } } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) hor[i][j + 1] += hor[i][j]; for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) ver[i][j + 1] += ver[i][j]; memset(dist, -1, sizeof(dist)); dist[0][0] = 0; queue<pair<int, int>> q; q.emplace(0, 0); while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); if (y == h - 1 && x == w - 1) break; if (x > 0 && hor[y][x - 1] > 0 && dist[y][x - 1] == -1) { dist[y][x - 1] = dist[y][x] + 1; q.emplace(y, x - 1); } if (x + 1 < w && hor[y][x] > 0 && dist[y][x + 1] == -1) { dist[y][x + 1] = dist[y][x] + 1; q.emplace(y, x + 1); } if (y > 0 && ver[x][y - 1] > 0 && dist[y - 1][x] == -1) { dist[y - 1][x] = dist[y][x] + 1; q.emplace(y - 1, x); } if (y + 1 < h && ver[x][y] > 0 && dist[y + 1][x] == -1) { dist[y + 1][x] = dist[y][x] + 1; q.emplace(y + 1, x); } } if (dist[h - 1][w - 1] == -1) { puts("Odekakedekinai.."); } else { cout << dist[h - 1][w - 1] << endl; } }