結果
| 問題 |
No.340 雪の足跡
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-05-27 16:41:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,112 bytes |
| コンパイル時間 | 637 ms |
| コンパイル使用メモリ | 60,608 KB |
| 実行使用メモリ | 7,880 KB |
| 最終ジャッジ日時 | 2024-10-07 16:28:33 |
| 合計ジャッジ時間 | 11,475 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 28 TLE * 4 |
ソースコード
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <cstdint>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
const int UP = 0;
const int DOWN = 1;
const int RIGHT = 2;
const int LEFT = 3;
const int dy[4] = { -1, 1, 0, 0 };
const int dx[4] = { 0, 0, 1, -1 };
struct state_t { int dist, v; };
int main() {
int w, h, n; scanf("%d%d%d", &w, &h, &n);
vector<uint8_t> g(h * w);
repeat (i,n) {
int m; scanf("%d", &m);
vector<int> b(m+1); repeat (i,m+1) scanf("%d", &b[i]);
repeat (i,m) {
int y0 = b[i] / w;
int y1 = b[i+1] / w;
int x0 = b[i] % w;
int x1 = b[i+1] % w;
if (y0 == y1) {
int y = y0;
repeat_from (z,min(x0,x1),max(x0,x1)) {
g[y * w + z ] |= (1 << RIGHT);
g[y * w + (z+1)] |= (1 << LEFT);
}
} else if (x0 == x1) {
int x = x0;
repeat_from (z,min(y0,y1),max(y0,y1)) {
g[ z * w + x] |= (1 << DOWN);
g[(z+1) * w + x] |= (1 << UP);
}
} else {
assert (false);
}
}
}
vector<int> dist(h * w);
queue<state_t> que;
dist[0] = 1;
que.push((state_t) { 1, 0 });
while (not que.empty()) {
state_t s = que.front(); que.pop();
int y = s.v / w;
int x = s.v % w;
repeat (i,4) {
if (not (g[s.v] & (1 << i))) continue; // the edge doesn't exist
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 or h <= ny or nx < 0 or w <= nx) continue;
state_t t = { s.dist + 1, ny * w + nx };
if (dist[t.v]) continue;
dist[t.v] = t.dist;
que.push(t);
}
}
if (dist[h * w - 1]) {
printf("%d\n", dist[h * w - 1] - 1);
} else {
puts("Odekakedekinai..");
}
return 0;
}