結果

問題 No.2731 Two Colors
ユーザー winguwingu
提出日時 2024-04-19 22:09:46
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 3,000 ms
コード長 2,872 bytes
コンパイル時間 3,822 ms
コンパイル使用メモリ 294,992 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2024-04-19 22:10:13
合計ジャッジ時間 8,432 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 238 ms
19,712 KB
testcase_01 AC 246 ms
19,712 KB
testcase_02 AC 243 ms
19,712 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 12 ms
5,376 KB
testcase_05 AC 10 ms
5,376 KB
testcase_06 AC 35 ms
6,784 KB
testcase_07 AC 34 ms
7,168 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 123 ms
16,768 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 167 ms
17,792 KB
testcase_12 AC 122 ms
16,768 KB
testcase_13 AC 131 ms
16,000 KB
testcase_14 AC 57 ms
9,472 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 60 ms
9,088 KB
testcase_17 AC 82 ms
11,648 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 67 ms
9,856 KB
testcase_20 AC 76 ms
11,648 KB
testcase_21 AC 17 ms
5,376 KB
testcase_22 AC 142 ms
17,792 KB
testcase_23 AC 35 ms
7,296 KB
testcase_24 AC 19 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 105 ms
14,720 KB
testcase_27 AC 137 ms
17,152 KB
testcase_28 AC 102 ms
12,672 KB
testcase_29 AC 32 ms
6,400 KB
testcase_30 AC 51 ms
8,704 KB
testcase_31 AC 36 ms
7,168 KB
testcase_32 AC 191 ms
21,632 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region template
#include<bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
using mint=modint998244353;
template<class T>inline bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}
#define rep1(i,a) for(int i=0;i<(int)(a);i++)
#define rep2(i,a,b) for(int i =(int)(a);i<(int)(b);i++)
#define rep3(i,a,b,c) for(int i=(int)(a);i<(int)(b);i+=(int)(c))
#define overloadRep(a,b,c,d,e,...)e
#define rep(...) overloadRep(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define drep(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define spa " "
#define yes "Yes"
#define no "No"
#define int long long
const int inf=8e18;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
using P=pair<int,int>;
using T=tuple<int,int,int>;
bool bit(int &x,int &p){return (x>>p)&1;}

#pragma endregion

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(15);

    int h,w;cin>>h>>w;
    vector<vector<int>>a(h,vector<int>(w));rep(i,h)rep(j,w)cin>>a[i][j];
    vector<vector<int>>col(h,vector<int>(w,-1));
    col[0][0]=0;col[h-1][w-1]=1;

    int now=false;
    set<T>st0,st1;
    vector used0(h,vector<bool>(w));
    vector used1(h,vector<bool>(w));
    st0.emplace(a[0][1],0,1);used0[0][1]=true;
    st0.emplace(a[1][0],1,0);used0[1][0]=true;
    st1.emplace(a[h-1][w-2],h-1,w-2);used1[h-1][w-2]=true;
    st1.emplace(a[h-2][w-1],h-2,w-1);used1[h-2][w-1]=true;

    int ans=0;

    while(true){
        ans++;
        if(!now){
            auto[val,i,j]=*st0.begin();
            //cout<<i<<spa<<j<<spa<<now<<spa<<a[i][j]<<endl;
            st0.erase(st0.begin());
            col[i][j]=now;
            rep(d,4){
                int ni=i+dx[d],nj=j+dy[d];
                if(ni<0 or ni>=h or nj<0 or nj>=w)continue;
                if(col[ni][nj]==now)continue;
                if(col[ni][nj]==1){cout<<ans<<endl;return 0;}
                if(used0[ni][nj])continue;
                st0.emplace(a[ni][nj],ni,nj);
                used0[ni][nj]=true;
            }
        }else{
            auto[val,i,j]=*st1.begin();
            //cout<<i<<spa<<j<<spa<<now<<spa<<a[i][j]<<endl;            
            st1.erase(st1.begin());
            col[i][j]=now;
            rep(d,4){
                int ni=i+dx[d],nj=j+dy[d];
                if(ni<0 or ni>=h or nj<0 or nj>=w)continue;
                if(col[ni][nj]==now)continue;
                if(col[ni][nj]==0){cout<<ans<<endl;return 0;}
                if(used1[ni][nj])continue;
                st1.emplace(a[ni][nj],ni,nj);
                used1[ni][nj]=true;
            }            
        }
        now^=1;
    }
    return 0;
}
0