結果

問題 No.2731 Two Colors
ユーザー ooaiu
提出日時 2024-08-10 17:57:57
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 436 ms / 3,000 ms
コード長 1,886 bytes
コンパイル時間 3,108 ms
コンパイル使用メモリ 256,076 KB
実行使用メモリ 11,516 KB
最終ジャッジ日時 2024-08-10 17:58:09
合計ジャッジ時間 11,823 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename T>
using pq = priority_queue<T>;
template<typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
void solve() {
    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];
    pqg<array<int, 3>> pq1, pq2;
    vector color(h, vector<int>(w, -1));
    pq1.push({0, 0, 0}); pq2.push({0, h - 1, w - 1});
    int count = 0;
    while(1) {
        bool f = false;
        if((count & 1) == 0) {
            auto[A, i, j] = pq1.top(); pq1.pop();
            if(color[i][j] != -1) continue;
            color[i][j] = 1;
            for(int r = 0; r < 4; r++) {
                int ii = i + dx[r], jj = j + dy[r];
                if(ii < 0 || ii >= h || jj < 0 || jj >= w) continue;
                if(color[ii][jj] == 0) {
                    f = true;
                    break;
                }else if(color[ii][jj] == -1){
                    pq1.push({a[ii][jj], ii, jj});
                }
            }
        }else {
            auto[A, i, j] = pq2.top(); pq2.pop();
            if(color[i][j] != -1) continue;
            color[i][j] = 0;
            for(int r = 0; r < 4; r++) {
                int ii = i + dx[r], jj = j + dy[r];
                if(ii < 0 || ii >= h || jj < 0 || jj > w) continue;
                if(color[ii][jj] == 1) {
                    f = true;
                    break;
                }else if(color[ii][jj] == -1){
                    pq2.push({a[ii][jj], ii, jj});
                }
            }
        }
        count++;
        if(f) break;
    }
    cout << count - 2 << endl;
}

int main() {
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
0