結果

問題 No.2731 Two Colors
ユーザー FplusFplusFFplusFplusF
提出日時 2024-04-05 11:44:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 3,000 ms
コード長 1,878 bytes
コンパイル時間 3,709 ms
コンパイル使用メモリ 291,388 KB
実行使用メモリ 21,376 KB
最終ジャッジ日時 2024-04-14 14:26:30
合計ジャッジ時間 9,263 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 308 ms
19,200 KB
testcase_01 AC 307 ms
19,328 KB
testcase_02 AC 306 ms
19,328 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 13 ms
6,944 KB
testcase_05 AC 11 ms
6,940 KB
testcase_06 AC 42 ms
7,040 KB
testcase_07 AC 38 ms
6,944 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 142 ms
16,384 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 204 ms
17,664 KB
testcase_12 AC 137 ms
16,512 KB
testcase_13 AC 168 ms
15,744 KB
testcase_14 AC 65 ms
9,600 KB
testcase_15 AC 8 ms
6,940 KB
testcase_16 AC 77 ms
9,088 KB
testcase_17 AC 99 ms
11,520 KB
testcase_18 AC 19 ms
6,940 KB
testcase_19 AC 81 ms
9,856 KB
testcase_20 AC 84 ms
11,776 KB
testcase_21 AC 21 ms
6,944 KB
testcase_22 AC 171 ms
17,664 KB
testcase_23 AC 44 ms
7,168 KB
testcase_24 AC 22 ms
6,944 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 122 ms
14,592 KB
testcase_27 AC 159 ms
17,152 KB
testcase_28 AC 120 ms
12,460 KB
testcase_29 AC 37 ms
6,940 KB
testcase_30 AC 59 ms
8,576 KB
testcase_31 AC 47 ms
6,944 KB
testcase_32 AC 231 ms
21,376 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> inline bool chmin(T &a,T b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T &a,T b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll h,w;
    cin >> h >> w;
    vector<vector<ll>> a(h,vector<ll>(w));
    rep(i,h){
        rep(j,w){
            cin >> a[i][j];
        }
    }
    vector<vector<ll>> c(h,vector<ll>(w,-1));
    vector<ll> pi={0,1,0,-1},pj={1,0,-1,0};
    set<tll> st_0,st_1;
    rep(k,4){
        ll ti=0+pi[k],tj=0+pj[k];
        if(ti<0||h<=ti||tj<0||w<=tj) continue;
        st_0.insert({a[ti][tj],ti,tj});
    }
    c[0][0]=0;

    rep(k,4){
        ll ti=h-1+pi[k],tj=w-1+pj[k];
        if(ti<0||h<=ti||tj<0||w<=tj) continue;
        st_1.insert({a[ti][tj],ti,tj});
    }
    c[h-1][w-1]=1;
    ll x=0,ans=0;
    while(true){
        ans++;
        ll v,si,sj;
        if(x==0){
            tie(v,si,sj)=*begin(st_0);
            c[si][sj]=0;
        }
        if(x==1){
            tie(v,si,sj)=*begin(st_1);
            c[si][sj]=1;
        }
        st_0.erase({v,si,sj});
        st_1.erase({v,si,sj});
        rep(k,4){
            ll ti=si+pi[k],tj=sj+pj[k];
            if(ti<0||h<=ti||tj<0||w<=tj) continue;
            if(c[ti][tj]!=-1&&c[si][sj]==1-c[ti][tj]){
                cout << ans << '\n';
                return 0;
            }
            if(c[ti][tj]!=-1) continue;
            if(x==0) st_0.insert({a[ti][tj],ti,tj});
            if(x==1) st_1.insert({a[ti][tj],ti,tj});
        }
        x++;
        x%=2;
    }
}
0