結果

問題 No.2731 Two Colors
ユーザー Today03Today03
提出日時 2024-04-19 21:48:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 471 ms / 3,000 ms
コード長 1,509 bytes
コンパイル時間 2,582 ms
コンパイル使用メモリ 215,360 KB
実行使用メモリ 10,892 KB
最終ジャッジ日時 2024-04-19 21:49:00
合計ジャッジ時間 9,226 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 464 ms
7,808 KB
testcase_01 AC 449 ms
7,776 KB
testcase_02 AC 471 ms
7,680 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 22 ms
6,944 KB
testcase_05 AC 13 ms
6,940 KB
testcase_06 AC 56 ms
6,944 KB
testcase_07 AC 69 ms
6,940 KB
testcase_08 AC 10 ms
6,940 KB
testcase_09 AC 258 ms
7,504 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 246 ms
10,892 KB
testcase_12 AC 253 ms
7,500 KB
testcase_13 AC 201 ms
7,672 KB
testcase_14 AC 111 ms
6,944 KB
testcase_15 AC 9 ms
6,940 KB
testcase_16 AC 96 ms
6,940 KB
testcase_17 AC 142 ms
6,944 KB
testcase_18 AC 26 ms
6,940 KB
testcase_19 AC 98 ms
6,944 KB
testcase_20 AC 172 ms
6,944 KB
testcase_21 AC 27 ms
6,944 KB
testcase_22 AC 260 ms
8,568 KB
testcase_23 AC 61 ms
6,940 KB
testcase_24 AC 31 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 212 ms
7,044 KB
testcase_27 AC 281 ms
8,452 KB
testcase_28 AC 157 ms
7,192 KB
testcase_29 AC 49 ms
6,940 KB
testcase_30 AC 91 ms
6,944 KB
testcase_31 AC 55 ms
6,944 KB
testcase_32 AC 305 ms
9,216 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {1, 0, -1, 0};

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];
        }
    }

    vector<priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>>> pq(2);
    pq[0].push({A[0][0], 0, 0});
    pq[1].push({A[H - 1][W - 1], H - 1, W - 1});

    vector<vector<vector<bool>>> vst(2, vector<vector<bool>>(H, vector<bool>(W, false)));

    int ans = 0;
    bool fin = false;

    int i = 0;
    while (true) {
        auto [a, x, y] = pq[i % 2].top();
        pq[i % 2].pop();

        if (vst[i % 2][x][y]) {
            continue;
        }

        vst[i % 2][x][y] = true;

        for (int j = 0; j < 4; j++) {
            int nx = x + dx[j];
            int ny = y + dy[j];

            if (nx < 0 || nx >= H || ny < 0 || ny >= W) {
                continue;
            }
            if (vst[i % 2][nx][ny]) {
                continue;
            }

            if (vst[(i + 1) % 2][nx][ny]) {
                ans = i - 1;
                fin = true;
                break;
            }

            pq[i % 2].push({A[nx][ny], nx, ny});
        }

        if (fin) {
            break;
        }

        i++;
    }

    cout << ans << endl;
}
0