結果

問題 No.2731 Two Colors
ユーザー Tatsu_mrTatsu_mr
提出日時 2024-04-19 22:05:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 413 ms / 3,000 ms
コード長 1,735 bytes
コンパイル時間 2,765 ms
コンパイル使用メモリ 216,268 KB
実行使用メモリ 11,684 KB
最終ジャッジ日時 2024-04-19 22:05:45
合計ジャッジ時間 9,045 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 413 ms
11,648 KB
testcase_01 AC 404 ms
11,684 KB
testcase_02 AC 404 ms
11,648 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 22 ms
5,376 KB
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 54 ms
5,376 KB
testcase_07 AC 69 ms
5,376 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 265 ms
9,216 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 223 ms
8,768 KB
testcase_12 AC 268 ms
9,216 KB
testcase_13 AC 190 ms
7,920 KB
testcase_14 AC 115 ms
6,144 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 90 ms
5,760 KB
testcase_17 AC 134 ms
6,656 KB
testcase_18 AC 23 ms
5,376 KB
testcase_19 AC 95 ms
5,852 KB
testcase_20 AC 173 ms
7,296 KB
testcase_21 AC 27 ms
5,376 KB
testcase_22 AC 252 ms
9,436 KB
testcase_23 AC 61 ms
5,376 KB
testcase_24 AC 32 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 207 ms
8,064 KB
testcase_27 AC 256 ms
9,672 KB
testcase_28 AC 161 ms
7,336 KB
testcase_29 AC 48 ms
5,376 KB
testcase_30 AC 90 ms
5,504 KB
testcase_31 AC 51 ms
5,376 KB
testcase_32 AC 301 ms
10,296 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> di = {-1, 0, 1, 0};
vector<int> dj = {0, 1, 0, -1};

int main() {
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }
    auto inc = [&](int i, int j) {
        return (0 <= i && i < h && 0 <= j && j < w);
    };
    vector<vector<int>> b(h, vector<int>(w, -1));
    vector<vector<vector<bool>>> visit(2, vector<vector<bool>>(h, vector<bool>(w, false)));
    b[0][0] = 1;
    visit[1][0][0] = true;
    b[h - 1][w - 1] = 0;
    visit[0][h - 1][w - 1] = true;
    vector<priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>>> pq(2);
    pq[1].push({a[0][1], 0, 1});
    visit[1][0][1] = true;
    pq[1].push({a[1][0], 1, 0});
    visit[1][1][0] = true;
    pq[0].push({a[h - 1][w - 2], h - 1, w - 2});
    visit[0][h - 1][w - 2] = true;
    pq[0].push({a[h - 2][w - 1], h - 2, w - 1});
    visit[0][h - 2][w - 1] = true;
    int cnt = 0;
    while (true) {
        cnt++;
        int id = cnt % 2;
        auto [x, i, j] = pq[id].top();
        pq[id].pop();
        b[i][j] = id;
        bool ok = true;
        for (int k = 0; k < 4; k++) {
            int ni = i + di[k], nj = j + dj[k];
            if (!inc(ni, nj)) {
                continue;
            }
            if (b[ni][nj] == 1 - id) {
                ok = false;
            } else if (b[ni][nj] == -1 && !visit[id][ni][nj]) {
                visit[id][ni][nj] = true;
                pq[id].push({a[ni][nj], ni, nj});
            }
        }
        if (!ok) {
            break;
        }
    }
    cout << cnt << endl;
}
0