結果

問題 No.124 門松列(3)
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-17 18:29:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,633 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 173,152 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-26 03:46:06
合計ジャッジ時間 3,183 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 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 3 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 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
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[i][j];
        }
    }

    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