結果

問題 No.2731 Two Colors
ユーザー 🦠みどりむし🦠みどりむし
提出日時 2024-04-04 23:54:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,635 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 102,916 KB
実行使用メモリ 24,896 KB
最終ジャッジ日時 2024-04-14 14:26:21
合計ジャッジ時間 10,850 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

int main() {
    int h, w; std::cin >> h >> w;
    std::vector<std::vector<int>> a(h);
    for(auto& row : a) {
        row.resize(w);
        for(auto& v : row) std::cin >> v;
    }

    using P = std::pair<int, std::pair<int, int>>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> que[2];
    que[0].emplace(a[0][1], std::make_pair(0, 1));
    que[0].emplace(a[1][0], std::make_pair(1, 0));
    que[1].emplace(a[h - 1][w - 2], std::make_pair(h - 1, w - 2));
    que[1].emplace(a[h - 2][w - 1], std::make_pair(h - 2, w - 1));

    std::vector<std::vector<int>> color(h);
    for(auto& row : color) {
        row.resize(w, -1);
    }
    color[0][0] = 0;
    color[h - 1][w - 1] = 1;

    for(int t = 0; ;) {
        auto& cands = que[t % 2];
        auto [ _, p ] = cands.top(); cands.pop();
        auto [ i, j ] = p;
        if(color[i][j] >= 0) continue;
        color[i][j] = t % 2;
        std::cerr << t << " " << i << " " << j << "\n";

        constexpr std::pair<int, int> DIRS[] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };

        for(const auto& [ di, dj ] : DIRS) {
            if(i + di >= h || i + di < 0) continue;
            if(j + dj >= w || j + dj < 0) continue;

            if(color[i + di][j + dj] == ((t + 1) % 2)) {
                std::cout << t + 1 << "\n";
                return 0;
            }

            if(color[i + di][j + dj] == -1) {
                std::cerr << i + di << " " << j + dj << "\n";
                cands.emplace(a[i + di][j + dj], std::make_pair(i + di, j + dj));
            }
        }

        ++t;
    }
}
0