結果

問題 No.124 門松列(3)
ユーザー ふーらくたるふーらくたる
提出日時 2016-09-16 14:29:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,222 bytes
コンパイル時間 635 ms
コンパイル使用メモリ 74,072 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 14:34:35
合計ジャッジ時間 1,600 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <tuple>
#include <functional>
#include <vector>
#include <queue>
using namespace std;

using State = tuple<int, int, int, int>; // (移動回数, 行, 列, 直前の数字)

const int MAX_W = 110;
const int MAX_H = 110;
const int INF = (1 << 28);

int dist[MAX_H][MAX_W][10];
int m[MAX_H][MAX_W];

int dr[4] = {1, 0, -1, 0},
    dc[4] = {0, 1, 0, -1};

int w, h;

bool inside(int r, int c) {
    return 0 <= r && r < h && 0 <= c && c < w;
}

int main() {
    cin >> w >> h;

    for (int r = 0; r < h; r++) {
        for (int c = 0; c < w; c++) {
            cin >> m[r][c];
        }
    }

    for (int r = 0; r < h; r++) {
        for (int c = 0; c < w; c++) {
            for (int d = 0; d < 10; d++) {
                dist[r][c][d] = INF;
            }
        }
    }

    priority_queue<State, vector<State>, greater<State>> que;
    if (m[0][1] != m[0][0]) {
        dist[0][1][m[0][0]] = 1;
        que.push(State(1, 0, 1, m[0][0]));
    }
    if (m[1][0] != m[0][0]) {
        dist[1][0][m[0][0]] = 1;
        que.push(State(1, 1, 0, m[0][0]));
    }
    while (!que.empty()) {
        State s = que.top(); que.pop();
        int cost = get<0>(s),
            row = get<1>(s),
            col = get<2>(s),
            prev = get<3>(s);
        if (dist[row][col][prev] < cost) continue;

        // ゴールできたら終了
        if (row == h - 1 && col == w - 1) {
            cout << cost << endl;
            return 0;
        }

        int cur = m[row][col];
        for (int i = 0; i < 4; i++) {
            int nr = row + dr[i], nc = col + dc[i];
            if (inside(nr, nc)) {
                // 門松列の条件を満たしているか
                int next = m[nr][nc];
                if (prev == next || dist[nr][nc][cur] <= cost + 1) continue;

                if (cur > prev && cur > next) {
                    dist[nr][nc][cur] = cost + 1;
                    que.push(State(cost + 1, nr, nc, cur));
                } else if (cur < prev && cur < next) {
                    dist[nr][nc][cur] = cost + 1;
                    que.push(State(cost + 1, nr, nc, cur));
                }
            }
        }
    }
    cout << -1 << endl;

    return 0;
}
0