結果

問題 No.2731 Two Colors
ユーザー ococonomy1ococonomy1
提出日時 2024-04-19 21:43:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 3,000 ms
コード長 5,382 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 123,400 KB
実行使用メモリ 11,572 KB
最終ジャッジ日時 2024-04-19 21:44:19
合計ジャッジ時間 5,158 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
11,008 KB
testcase_01 AC 216 ms
11,136 KB
testcase_02 AC 219 ms
11,136 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 24 ms
5,376 KB
testcase_07 AC 28 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 97 ms
9,724 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 105 ms
11,400 KB
testcase_12 AC 93 ms
9,324 KB
testcase_13 AC 87 ms
9,016 KB
testcase_14 AC 45 ms
5,984 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 43 ms
6,076 KB
testcase_17 AC 59 ms
7,116 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 43 ms
6,132 KB
testcase_20 AC 63 ms
7,488 KB
testcase_21 AC 14 ms
5,376 KB
testcase_22 AC 104 ms
10,284 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 87 ms
8,704 KB
testcase_27 AC 101 ms
10,488 KB
testcase_28 AC 67 ms
8,312 KB
testcase_29 AC 22 ms
5,376 KB
testcase_30 AC 37 ms
6,404 KB
testcase_31 AC 25 ms
5,400 KB
testcase_32 AC 126 ms
11,572 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <climits>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define TEST cerr << "TEST" << endl
#define AMARI 998244353
//#define AMARI 1000000007
#define el '\n'
#define El '\n'

#define MULTI_TEST_CASE false
void solve(void){
    //問題を見たらまず「この問題設定から言えること」をいっぱい言う
    //一個回答に繋がりそうな解法が見えても、実装や細かい詰めに時間がかかりそうなら別の方針を考えてみる
    //添え字回りで面倒になりそうなときは楽になる言い換えを実装の前にじっくり考える
    //ある程度考察しても全然取っ掛かりが見えないときは実験をしてみる
    //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
    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];
            a[i][j] *= -1;
        }
    }
    priority_queue<pair<int,pii>> color0,color1;
    color0.push(pair(a[0][1],pair(0,1)));
    color0.push(pair(a[1][0],pair(1,0)));
    color1.push(pair(a[h - 2][w - 1],pair(h - 2,w - 1)));
    color1.push(pair(a[h - 1][w - 2],pair(h - 1,w - 2)));
    vector<vector<int>> color(h,vector<int>(w,-1));
    color[0][0] = 0;
    color[h - 1][w - 1] = 1;

    for(int i = 0; i < h * w; i++){
        if(i % 2 == 0){
            while(1){
                pii temp = color0.top().second;
                color0.pop();
                if(color[temp.first][temp.second] != -1)continue;

                color[temp.first][temp.second] = 0;
                if(temp.first != 0 && color[temp.first - 1][temp.second] == 1){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.first != h - 1 && color[temp.first + 1][temp.second] == 1){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.second != 0 && color[temp.first][temp.second - 1] == 1){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.second != w - 1 && color[temp.first][temp.second + 1] == 1){
                    cout << i + 1 << el;
                    return;
                }


                if(temp.first != 0 && color[temp.first - 1][temp.second] == -1)color0.push(pair(a[temp.first - 1][temp.second],pair(temp.first - 1,temp.second)));
                if(temp.first != h - 1 && color[temp.first + 1][temp.second] == -1)color0.push(pair(a[temp.first + 1][temp.second],pair(temp.first + 1,temp.second)));
                if(temp.second != 0 && color[temp.first][temp.second - 1] == -1)color0.push(pair(a[temp.first][temp.second - 1],pair(temp.first,temp.second - 1)));
                if(temp.second != w - 1 && color[temp.first][temp.second + 1] == -1)color0.push(pair(a[temp.first][temp.second + 1],pair(temp.first,temp.second + 1)));

                break;
            }
        }

        if(i % 2 == 1){

            while(1){
                pii temp = color1.top().second;
                color1.pop();
                if(color[temp.first][temp.second] != -1)continue;

                color[temp.first][temp.second] = 1;
                if(temp.first != 0 && color[temp.first - 1][temp.second] == 0){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.first != h - 1 && color[temp.first + 1][temp.second] == 0){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.second != 0 && color[temp.first][temp.second - 1] == 0){
                    cout << i + 1 << el;
                    return;
                }
                if(temp.second != w - 1 && color[temp.first][temp.second + 1] == 0){
                    cout << i + 1 << el;
                    return;
                }


                if(temp.first != 0 && color[temp.first - 1][temp.second] == -1)color1.push(pair(a[temp.first - 1][temp.second],pair(temp.first - 1,temp.second)));
                if(temp.first != h - 1 && color[temp.first + 1][temp.second] == -1)color1.push(pair(a[temp.first + 1][temp.second],pair(temp.first + 1,temp.second)));
                if(temp.second != 0 && color[temp.first][temp.second - 1] == -1)color1.push(pair(a[temp.first][temp.second - 1],pair(temp.first,temp.second - 1)));
                if(temp.second != w - 1 && color[temp.first][temp.second + 1] == -1)color1.push(pair(a[temp.first][temp.second + 1],pair(temp.first,temp.second + 1)));

                break;
            }
        }
    }
    assert(0);
    return;
}

void calc(void){
    return;
}

signed main(void){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE)cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
0