結果

問題 No.2731 Two Colors
ユーザー 🦠みどりむし🦠みどりむし
提出日時 2024-04-05 00:02:37
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 467 ms / 3,000 ms
コード長 1,642 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 102,908 KB
実行使用メモリ 12,856 KB
最終ジャッジ日時 2024-04-14 14:26:21
合計ジャッジ時間 8,592 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 466 ms
11,392 KB
testcase_01 AC 465 ms
11,392 KB
testcase_02 AC 467 ms
11,392 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 23 ms
6,940 KB
testcase_05 AC 15 ms
6,940 KB
testcase_06 AC 60 ms
6,940 KB
testcase_07 AC 75 ms
6,940 KB
testcase_08 AC 11 ms
6,940 KB
testcase_09 AC 278 ms
9,568 KB
testcase_10 AC 6 ms
6,940 KB
testcase_11 AC 245 ms
12,856 KB
testcase_12 AC 278 ms
9,556 KB
testcase_13 AC 206 ms
9,112 KB
testcase_14 AC 121 ms
6,940 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 99 ms
6,944 KB
testcase_17 AC 151 ms
7,116 KB
testcase_18 AC 25 ms
6,944 KB
testcase_19 AC 103 ms
6,940 KB
testcase_20 AC 184 ms
7,552 KB
testcase_21 AC 28 ms
6,944 KB
testcase_22 AC 274 ms
10,376 KB
testcase_23 AC 66 ms
6,944 KB
testcase_24 AC 33 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 225 ms
8,548 KB
testcase_27 AC 279 ms
10,460 KB
testcase_28 AC 169 ms
8,288 KB
testcase_29 AC 51 ms
6,944 KB
testcase_30 AC 99 ms
6,940 KB
testcase_31 AC 58 ms
6,940 KB
testcase_32 AC 331 ms
11,476 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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