結果

問題 No.340 雪の足跡
ユーザー pekempeypekempey
提出日時 2016-11-21 04:40:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 1,000 ms
コード長 2,197 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 173,528 KB
実行使用メモリ 15,324 KB
最終ジャッジ日時 2023-08-18 06:06:54
合計ジャッジ時間 4,665 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,488 KB
testcase_01 AC 4 ms
9,496 KB
testcase_02 AC 6 ms
7,232 KB
testcase_03 AC 4 ms
9,552 KB
testcase_04 AC 4 ms
9,540 KB
testcase_05 AC 5 ms
7,224 KB
testcase_06 AC 4 ms
9,484 KB
testcase_07 AC 3 ms
7,428 KB
testcase_08 AC 4 ms
9,728 KB
testcase_09 AC 5 ms
9,468 KB
testcase_10 AC 4 ms
9,604 KB
testcase_11 AC 4 ms
9,612 KB
testcase_12 AC 5 ms
11,472 KB
testcase_13 AC 5 ms
11,532 KB
testcase_14 AC 8 ms
13,604 KB
testcase_15 AC 9 ms
13,596 KB
testcase_16 AC 7 ms
13,636 KB
testcase_17 AC 10 ms
13,532 KB
testcase_18 AC 9 ms
13,640 KB
testcase_19 AC 10 ms
13,600 KB
testcase_20 AC 38 ms
15,112 KB
testcase_21 AC 23 ms
11,832 KB
testcase_22 AC 14 ms
15,028 KB
testcase_23 AC 47 ms
15,112 KB
testcase_24 AC 30 ms
15,164 KB
testcase_25 AC 32 ms
15,048 KB
testcase_26 AC 11 ms
13,648 KB
testcase_27 AC 6 ms
11,656 KB
testcase_28 AC 11 ms
13,840 KB
testcase_29 AC 73 ms
15,056 KB
testcase_30 AC 48 ms
13,524 KB
testcase_31 AC 84 ms
14,816 KB
testcase_32 AC 101 ms
15,168 KB
testcase_33 AC 86 ms
15,168 KB
testcase_34 AC 105 ms
15,036 KB
testcase_35 AC 55 ms
15,060 KB
testcase_36 AC 55 ms
15,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

int hor[1001][1001];
int 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;
    }
}
0