結果

問題 No.2731 Two Colors
ユーザー pia-19pia-19
提出日時 2024-04-19 23:28:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 365 ms / 3,000 ms
コード長 2,191 bytes
コンパイル時間 3,585 ms
コンパイル使用メモリ 288,768 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2024-04-19 23:28:19
合計ジャッジ時間 9,972 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 362 ms
15,616 KB
testcase_01 AC 359 ms
15,360 KB
testcase_02 AC 365 ms
15,360 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 21 ms
6,944 KB
testcase_05 AC 12 ms
6,944 KB
testcase_06 AC 51 ms
6,940 KB
testcase_07 AC 66 ms
6,940 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 267 ms
11,604 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 208 ms
10,108 KB
testcase_12 AC 244 ms
11,392 KB
testcase_13 AC 180 ms
9,312 KB
testcase_14 AC 110 ms
7,040 KB
testcase_15 AC 9 ms
6,940 KB
testcase_16 AC 90 ms
6,944 KB
testcase_17 AC 138 ms
7,552 KB
testcase_18 AC 22 ms
6,944 KB
testcase_19 AC 95 ms
6,944 KB
testcase_20 AC 165 ms
8,720 KB
testcase_21 AC 23 ms
6,940 KB
testcase_22 AC 243 ms
11,372 KB
testcase_23 AC 58 ms
6,940 KB
testcase_24 AC 30 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 207 ms
9,948 KB
testcase_27 AC 260 ms
11,808 KB
testcase_28 AC 142 ms
8,628 KB
testcase_29 AC 44 ms
6,940 KB
testcase_30 AC 88 ms
6,940 KB
testcase_31 AC 48 ms
6,944 KB
testcase_32 AC 289 ms
12,564 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int INF = 1<<30;
const int MOD = 998244353;
//const long long INF = 1LL<<60;
using graph = vector<vector<int>>;
using ll = long long;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

void print(vector<vector<int>> & vec, int h, int w){
    for (int i=0; i < h; i++){
        for (int j=0; j < w; j++){
            cout << vec[i][j];
        }
        cout << endl;
    }
}

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<vector<int>> region1(h, vector<int>(w)), region0(h, vector<int>(w));
    priority_queue<tuple<int, int, int>> pq1, pq0;
    pq1.push(make_tuple(-a[0][0], 0, 0));
    pq0.push(make_tuple(-a[h-1][w-1], h-1, w-1));
    
    for (int i=1; i <= h*w; i++){
        if (i % 2 == 1){
            int v, x, y;
            tie(v, x, y) = pq1.top();
            pq1.pop();
            if (region0[x][y] != 0){
                cout << i-2 << endl;
                return 0;
            }
            region1[x][y] = 2;
            for (int j=0; j < 4; j++){
                int nx = x + dx[j];
                int ny = y + dy[j];
                if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
                if (region1[nx][ny] == 0){
                    region1[nx][ny] = 1;
                    pq1.push(make_tuple(-a[nx][ny], nx, ny));
                }
                continue;
            }
        }
        if (i % 2 == 0){
            int v, x, y;
            tie(v, x, y) = pq0.top();
            pq0.pop();
            region0[x][y] = 2;
            //cout << i << x << y << v << endl;
            if (region1[x][y] != 0){
                cout << i-2 << endl;
                return 0;
            }
            for (int j=0; j < 4; j++){
                int nx = x + dx[j];
                int ny = y + dy[j];
                if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
                if (region0[nx][ny] == 0){
                    region0[nx][ny] = 1;
                    pq0.push(make_tuple(-a[nx][ny], nx, ny));
                }
            }
        }
    }
}
0