結果

問題 No.124 門松列(3)
ユーザー MisterMister
提出日時 2020-08-22 17:37:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,457 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 94,768 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 11:30:03
合計ジャッジ時間 2,222 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 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 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 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 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 <vector>
#include <queue>
#include <tuple>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

const std::vector<std::pair<int, int>> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool judge(int a, int b, int c) {
    if (a * b * c == 0) return true;
    return ((a < b && b > c) || (a > b && b < c)) && a != c;
}

void solve() {
    int h, w;
    std::cin >> w >> h;

    auto xss = vec(h, vec(w, 0));
    for (auto& xs : xss) {
        for (auto& x : xs) std::cin >> x;
    }

    auto dist = vec(10, vec(h, vec(w, -1)));
    dist[0][0][0] = 0;
    std::queue<std::tuple<int, int, int>> que;
    que.emplace(0, 0, 0);

    while (!que.empty()) {
        auto [p, x, y] = que.front();
        que.pop();

        for (auto [dx, dy] : dxys) {
            int nx = x + dx,
                ny = y + dy;

            if (nx < 0 || h <= nx ||
                ny < 0 || w <= ny ||
                dist[xss[x][y]][nx][ny] != -1 ||
                !judge(p, xss[x][y], xss[nx][ny])) continue;

            dist[xss[x][y]][nx][ny] = dist[p][x][y] + 1;
            que.emplace(xss[x][y], nx, ny);
        }
    }

    int ans = -1;
    for (auto& ds : dist) {
        int d = ds[h - 1][w - 1];
        if (ans == -1 || (d != -1 && d < ans)) ans = d;
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0