結果

問題 No.2731 Two Colors
ユーザー Tatsu_mrTatsu_mr
提出日時 2024-04-19 22:00:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,676 bytes
コンパイル時間 2,861 ms
コンパイル使用メモリ 213,028 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-04-19 22:00:41
合計ジャッジ時間 9,332 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 5 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 62 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 10 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 107 ms
5,976 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 204 ms
8,064 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 91 ms
5,376 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 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<bool>> visit(h, vector<bool>(w, false));
    b[0][0] = 1;
    visit[0][0] = true;
    b[h - 1][w - 1] = 0;
    visit[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[0][1] = true;
    pq[1].push({a[1][0], 1, 0});
    visit[1][0] = true;
    pq[0].push({a[h - 1][w - 2], h - 1, w - 2});
    visit[h - 1][w - 2] = true;
    pq[0].push({a[h - 2][w - 1], h - 2, w - 1});
    visit[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[ni][nj]) {
                visit[ni][nj] = true;
                pq[id].push({a[ni][nj], ni, nj});
            }
        }
        if (!ok) {
            break;
        }
    }
    cout << cnt << endl;
}
0