結果

問題 No.2731 Two Colors
ユーザー hamo21hamo21
提出日時 2024-04-19 21:49:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 469 ms / 3,000 ms
コード長 4,189 bytes
コンパイル時間 4,484 ms
コンパイル使用メモリ 267,456 KB
実行使用メモリ 21,376 KB
最終ジャッジ日時 2024-04-19 21:49:34
合計ジャッジ時間 11,608 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 469 ms
19,456 KB
testcase_01 AC 459 ms
19,456 KB
testcase_02 AC 455 ms
19,456 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 23 ms
6,944 KB
testcase_05 AC 15 ms
6,940 KB
testcase_06 AC 63 ms
6,940 KB
testcase_07 AC 73 ms
7,168 KB
testcase_08 AC 11 ms
6,944 KB
testcase_09 AC 279 ms
16,512 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 297 ms
18,176 KB
testcase_12 AC 273 ms
16,384 KB
testcase_13 AC 226 ms
15,872 KB
testcase_14 AC 122 ms
9,728 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 110 ms
9,344 KB
testcase_17 AC 155 ms
11,776 KB
testcase_18 AC 27 ms
6,940 KB
testcase_19 AC 112 ms
9,984 KB
testcase_20 AC 187 ms
11,904 KB
testcase_21 AC 29 ms
6,940 KB
testcase_22 AC 286 ms
17,664 KB
testcase_23 AC 69 ms
7,424 KB
testcase_24 AC 34 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 227 ms
14,592 KB
testcase_27 AC 286 ms
17,408 KB
testcase_28 AC 176 ms
12,928 KB
testcase_29 AC 58 ms
6,940 KB
testcase_30 AC 107 ms
8,832 KB
testcase_31 AC 65 ms
7,040 KB
testcase_32 AC 354 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,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef int64_t ll;
using mint=modint998244353;

typedef long double ld;
const ll MOD=1000000007;
const ll MODA=998244353;
ll vx[4]={0,1,0,-1};
ll vy[4]={1,0,-1,0};
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
long long gcd(long long a,long long b){
    a=abs(a);
    b=abs(b);
    ll gcdmax=max(a,b);
    ll gcdmin=min(a,b);
    if(gcdmin==0)return gcdmax;
    while(true){
        if(gcdmax%gcdmin==0)break;
        else gcdmax%=gcdmin;
        swap(gcdmin,gcdmax);
    }
    return gcdmin;
}
ll pow(ll N,ll P,ll M){
    if(P==0)return 1;
    else if(P%2==0){
        ll t=pow(N,P/2,M);
        return t*t%M;
    }
    else return N*pow(N,P-1,M)%M;
}
ll pow(ll N,ll P){
    if(P==0)return 1;
    else if(P%2==0){
        ll t=pow(N,P/2);
        return t*t;
    }
    else return N*pow(N,P-1);
}

vector<ll> fac;
vector<ll> finv;
vector<ll> inv;
void COMinit(ll N,ll P){
    rep(i,N+1){
        if(i==0){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else if(i==1){
            fac.push_back(1);
            finv.push_back(1);
            inv.push_back(1);
        }
        else{
            fac.push_back(fac.at(i-1)*i%P);
            inv.push_back(P-inv.at(P%i)*(P/i)%P);
            finv.push_back(finv.at(i-1)*inv.at(i)%P);
        }
    }
}

ll COM(ll n,ll k,ll P){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    return fac.at(n)*(finv.at(k)*finv.at(n-k)%P)%P;
}


struct UnionFind {
    vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化
        for(ll i = 0; i < N; i++) par[i] = i;
    }

    ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(ll x, ll y) { // xとyの木を併合
        ll rx = root(x); //xの根をrx
        ll ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
    }

    bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};

int main(){
    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));
    C[0][0]=1,C[H-1][W-1]=0;
    set<pair<ll,pair<ll,ll>>> S1;
    set<pair<ll,pair<ll,ll>>> S0;
    S1.insert({A[0][1],{0,1}});
    C[0][1]=C[1][0]=1;
    C[H-2][W-1]=C[H-1][W-2]=0;
    S1.insert({A[1][0],{1,0}});
    S0.insert({A[H-2][W-1],{H-2,W-1}});
    S0.insert({A[H-1][W-2],{H-1,W-2}});
    ll cnt=0;
    while(true){
        cnt++;
        if(cnt%2==1){
            pair<ll,pair<ll,ll>> p=(*S1.begin());
            //cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl;
            if(S0.count(p)){
                cout<<cnt<<endl;
                return 0;
            }
            S1.erase(p);
            rep(i,4){
                ll nx=p.second.first+vx[i];
                ll ny=p.second.second+vy[i];
                if(nx>=0&&nx<H&&ny>=0&&ny<W){
                    if(C[nx][ny]==1)continue;
                    C[nx][ny]=1;
                    S1.insert({A[nx][ny],{nx,ny}});
                }
            }
        }
        else{
            pair<ll,pair<ll,ll>> p=(*S0.begin());
            //cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl;
            if(S1.count(p)){
                cout<<cnt<<endl;
                return 0;
            }
            S0.erase(p);
            rep(i,4){
                ll nx=p.second.first+vx[i];
                ll ny=p.second.second+vy[i];
                if(nx>=0&&nx<H&&ny>=0&&ny<W){
                    if(C[nx][ny]==0)continue;
                    C[nx][ny]=0;
                    S0.insert({A[nx][ny],{nx,ny}});
                }
            }
        }
    }
}
0