結果

問題 No.124 門松列(3)
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-17 18:25:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,633 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 173,204 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-26 03:40:35
合計ジャッジ時間 3,333 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

int main() {
    int W, H, M[110][110] = {};
    cin >> W >> H;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> M[j][i];
        }
    }

    int dp[110][110][4] = {};
    for (int i = 0; i < 105; i++) {
        for (int j = 0; j < 105; j++) {
            for (int k = 0; k < 4; k++) {
                dp[i][j][k] = 1e5;
            }
        }
    }

    struct book {
        int x, y, bef;
    };

    dp[1][0][2] = 1;
    dp[0][1][1] = 1;

    queue<book> que;
    que.push({1, 0, 2});
    que.push({0, 1, 1});

    while (!que.empty()) {
        auto B = que.front();
        que.pop();
        int nx = B.x, ny = B.y, ns = B.bef;
        int bx = nx + dx[B.bef], by = ny + dy[B.bef];
        for (int i = 0; i < 4; i++) {
            int nxt_x = nx + dx[i], nxt_y = ny + dy[i];
            int nxt_step = dp[nx][ny][ns] + 1;
            if (nxt_x < 0 || nxt_x >= H || nxt_y < 0 || nxt_y >= W)
                continue;
            if (M[bx][by] == M[nxt_x][nxt_y])
                continue;
            if (dp[nxt_x][nxt_y][(i + 2) % 4] <= nxt_step)
                continue;
            if ((M[nx][ny] - M[bx][by]) * (M[nx][ny] - M[nxt_x][nxt_y]) <= 0)
                continue;
            dp[nxt_x][nxt_y][(i + 2) % 4] = nxt_step;
            que.push({nxt_x, nxt_y, (i + 2) % 4});
        }
    }

    int ans = 1e5;
    for (int i = 0; i < 4; i++) {
        ans = min(ans, dp[H - 1][W - 1][i]);
    }
    if (ans >= 1e5)
        ans = -1;

    cout << ans << endl;
}
0