結果

問題 No.340 雪の足跡
ユーザー pekempey
提出日時 2016-11-21 04:40:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 1,000 ms
コード長 2,197 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 175,036 KB
実行使用メモリ 15,260 KB
最終ジャッジ日時 2024-11-27 09:25:09
合計ジャッジ時間 4,232 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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