結果

問題 No.340 雪の足跡
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-05-21 08:38:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 1,000 ms
コード長 2,565 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 175,884 KB
実行使用メモリ 27,136 KB
最終ジャッジ日時 2024-04-19 08:30:47
合計ジャッジ時間 6,257 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 12 ms
6,400 KB
testcase_15 AC 15 ms
6,784 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 19 ms
6,912 KB
testcase_18 AC 15 ms
6,912 KB
testcase_19 AC 18 ms
6,912 KB
testcase_20 AC 152 ms
26,624 KB
testcase_21 AC 88 ms
5,376 KB
testcase_22 AC 31 ms
26,624 KB
testcase_23 AC 177 ms
26,772 KB
testcase_24 AC 141 ms
26,624 KB
testcase_25 AC 148 ms
26,584 KB
testcase_26 AC 24 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 22 ms
5,632 KB
testcase_29 AC 181 ms
21,120 KB
testcase_30 AC 123 ms
15,232 KB
testcase_31 AC 206 ms
25,088 KB
testcase_32 AC 238 ms
27,008 KB
testcase_33 AC 195 ms
27,004 KB
testcase_34 AC 239 ms
27,136 KB
testcase_35 AC 194 ms
27,008 KB
testcase_36 AC 198 ms
27,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

int W, H, N;
int e[1001][1001][2];  //(i, j) -> (i + 1, j), (i, j) -> (i, j + 1)

inline P toNode(int n) {
    return P(n / W, n % W);
}

const int INF = 99999999;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool inside(int x, int y) {
    return 0 <= x && x < H && 0 <= y && y < W;
}

bool canGo(int x1, int y1, int x2, int y2) {
    if (!inside(x1, y1)) return false;
    if (!inside(x2, y2)) return false;


    if (x1 > x2 || y1 > y2) {
        swap(x1, x2);
        swap(y1, y2);
    }
    // assert(x1 <= x2 && y1 <= y2);
    return e[x1][y1][x1 == x2];
}

int bfs(int sx, int sy, int gx, int gy) {
    queue<P> que;
    que.push(P(sx, sy));
    vector<vector<int>> d(H, vector<int>(W, INF));
    d[sx][sy] = 0;

    while (!que.empty()) {
        int x, y;
        tie(x, y) = que.front(); que.pop();
        if (x == gx && y == gy)
             break;

        rep(k, 4) {
            int nx = x + dx[k], ny = y + dy[k];
            if (!canGo(x, y, nx, ny)) continue;

            if (d[nx][ny] == INF) {
                que.push(P(nx, ny));
                d[nx][ny] = d[x][y] + 1;
            }
        }
    }
    return d[gx][gy];
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> W >> H >> N;
    rep(i, N) {
        int M;
        cin >> M;
        vector<int> B(M + 1);
        rep(j, M + 1) {
            cin >> B[j];
        }
        rep(j, M) {
            int x1, y1, x2, y2;
            tie(x1, y1) = toNode(B[j]);
            tie(x2, y2) = toNode(B[j + 1]);

            if (B[j] > B[j + 1]) {
                swap(x1, x2);
                swap(y1, y2);
            }
            if (y1 == y2) {
                e[x1][y1][0]++;
                e[x2][y2][0]--;
            } else {
                e[x1][y1][1]++;
                e[x2][y2][1]--;
            }
        }
    }

    rep(j, W) {
        rep(i, H) {
            e[i + 1][j][0] += e[i][j][0];
        }
    }

    rep(i, H) {
        rep(j, W) {
            e[i][j + 1][1] += e[i][j][1];
        }
    }

    int ans = bfs(0, 0, H - 1, W - 1);
    if (ans == INF) {
        cout << "Odekakedekinai.." << endl;
    } else {
        cout << ans << endl;
    }

}
0