結果

問題 No.2731 Two Colors
ユーザー AngrySadEightAngrySadEight
提出日時 2024-03-14 17:45:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,652 bytes
コンパイル時間 1,058 ms
コンパイル使用メモリ 91,164 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-14 14:25:45
合計ジャッジ時間 8,413 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
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 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

int main() {
    ll H, W;
    cin >> H >> W;
    vector<vector<ll>> A(H, vector<ll>(W));
    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {
            cin >> A[i][j];
        }
    }
    vector<vector<ll>> color(H, vector<ll>(W, -1));
    vector<priority_queue<pll, vector<pll>, greater<pll>>> pque(2);
    pque[0].push(pll(A[0][0], 0));
    pque[1].push(pll(A[H - 1][W - 1], (H - 1) * W + (W - 1)));
    vector<ll> dx = {1, 0, -1, 0};
    vector<ll> dy = {0, 1, 0, -1};
    ll cnt = 0;
    while (true) {
        bool finish = false;
        ll cnt_mod = cnt % 2;
        cnt++;
        pll p = pque[cnt_mod].top();
        pque[cnt_mod].pop();
        ll px = p.second / W;
        ll py = p.second % W;
        if (color[px][py] != -1) {
            continue;
        }
        color[px][py] = cnt_mod;
        for (ll i = 0; i < 4; i++) {
            if (px + dx[i] >= 0 && px + dx[i] < H && py + dy[i] >= 0 &&
                py + dy[i] < W) {
                ll new_c = color[px + dx[i]][py + dy[i]];
                if (new_c != -1) {
                    if (new_c != color[px][py]) {
                        finish = true;
                        break;
                    }
                } else {
                    pque[cnt_mod].push(pll(A[px + dx[i]][py + dy[i]],
                                           (px + dx[i]) * W + (py + dy[i])));
                }
            }
        }
        if (finish) {
            break;
        }
    }
    cout << cnt - 2 << endl;
}
0