結果

問題 No.340 雪の足跡
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-05-21 07:33:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 201 ms / 1,000 ms
コード長 2,617 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 169,972 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2024-04-16 04:40:57
合計ジャッジ時間 5,852 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 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 2 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
5,376 KB
testcase_15 AC 14 ms
5,504 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 17 ms
5,760 KB
testcase_18 AC 15 ms
5,760 KB
testcase_19 AC 18 ms
5,760 KB
testcase_20 AC 152 ms
14,976 KB
testcase_21 AC 98 ms
5,376 KB
testcase_22 AC 20 ms
14,848 KB
testcase_23 AC 163 ms
14,940 KB
testcase_24 AC 137 ms
14,976 KB
testcase_25 AC 139 ms
14,972 KB
testcase_26 AC 25 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 22 ms
5,376 KB
testcase_29 AC 165 ms
13,280 KB
testcase_30 AC 113 ms
9,472 KB
testcase_31 AC 174 ms
14,336 KB
testcase_32 AC 201 ms
15,360 KB
testcase_33 AC 164 ms
15,204 KB
testcase_34 AC 199 ms
15,192 KB
testcase_35 AC 176 ms
15,232 KB
testcase_36 AC 176 ms
15,360 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:68:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |     scanf("%d %d %d", &W, &H, &N);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:73:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |         scanf("%d", &M);
      |         ~~~~~^~~~~~~~~~
main.cpp:78:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |             scanf("%d", &B[j]);
      |             ~~~~~^~~~~~~~~~~~~

ソースコード

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 (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 (inside(nx, ny) && 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;
    scanf("%d %d %d", &W, &H, &N);

    rep(i, N) {
        int M;
        // cin >> M;
        scanf("%d", &M);

        vector<int> B(M + 1);
        rep(j, M + 1) {
            // cin >> B[j];
            scanf("%d", &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