結果
問題 |
No.340 雪の足跡
|
ユーザー |
![]() |
提出日時 | 2019-08-21 14:39:15 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,151 bytes |
コンパイル時間 | 1,255 ms |
コンパイル使用メモリ | 87,344 KB |
実行使用メモリ | 15,488 KB |
最終ジャッジ日時 | 2024-10-08 18:06:11 |
合計ジャッジ時間 | 8,812 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 31 WA * 1 |
ソースコード
#include <algorithm> #include <iostream> #include <vector> #include <queue> #include <tuple> using namespace std; int main() { int w, h, n; cin >> w >> h >> n; vector<vector<int>> row(h, vector<int>(w + 1)); vector<vector<int>> col(h + 1, vector<int>(w)); while (n--) { int m; cin >> m; vector<int> b(m + 1); for (int &bi: b) cin >> bi; for (int i = 0; i < m; i++) { int p0 = b[i], p1 = b[i + 1]; int i0 = p0 / w, j0 = p0 % w; int i1 = p1 / w, j1 = p1 % w; if (i0 == i1) { if (j0 > j1) swap(j0, j1); j1++; row[i0][j0]++; row[i1][j1]--; } else { if (i0 > i1) swap(i0, i1); i1++; col[i0][j0]++; col[i1][j1]--; } } } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { row[i][j + 1] += row[i][j]; } for (int j = 0; j < w; j++) for (int i = 0; i < h; i++) { col[i + 1][j] += col[i][j]; } vector<vector<int>> dist(h, vector<int>(w, -1)); queue<pair<int, int>> que; dist[0][0] = 0; que.emplace(0, 0); while (!que.empty()) { int i, j; tie(i, j) = que.front(); que.pop(); if (row[i][j]) { if (j - 1 >= 0 && row[i][j - 1] && dist[i][j - 1] == -1) { dist[i][j - 1] = dist[i][j] + 1; que.emplace(i, j - 1); } if (j + 1 < w && row[i][j + 1] && dist[i][j + 1] == -1) { dist[i][j + 1] = dist[i][j] + 1; que.emplace(i, j + 1); } } if (col[i][j]) { if (i - 1 >= 0 && col[i - 1][j] && dist[i - 1][j] == -1) { dist[i - 1][j] = dist[i][j] + 1; que.emplace(i - 1, j); } if (i + 1 < h && col[i + 1][j] && dist[i + 1][j] == -1) { dist[i + 1][j] = dist[i][j] + 1; que.emplace(i + 1, j); } } } int res = dist[h - 1][w - 1]; if (res == -1) { cout << "Odekakedekinai.." << endl; } else { cout << res << endl; } return 0; }