結果

問題 No.2731 Two Colors
ユーザー umezoumezo
提出日時 2024-04-19 22:53:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 255 ms / 3,000 ms
コード長 2,076 bytes
コンパイル時間 3,994 ms
コンパイル使用メモリ 284,656 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2024-04-19 22:53:31
合計ジャッジ時間 8,030 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 255 ms
26,576 KB
testcase_01 AC 241 ms
26,652 KB
testcase_02 AC 245 ms
26,880 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 29 ms
6,948 KB
testcase_07 AC 32 ms
7,812 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 110 ms
19,592 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 129 ms
20,048 KB
testcase_12 AC 106 ms
19,556 KB
testcase_13 AC 106 ms
15,772 KB
testcase_14 AC 49 ms
10,356 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 49 ms
9,224 KB
testcase_17 AC 67 ms
12,148 KB
testcase_18 AC 13 ms
5,376 KB
testcase_19 AC 51 ms
9,472 KB
testcase_20 AC 68 ms
13,968 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 113 ms
19,880 KB
testcase_23 AC 30 ms
7,328 KB
testcase_24 AC 17 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 87 ms
16,564 KB
testcase_27 AC 113 ms
20,420 KB
testcase_28 AC 79 ms
14,184 KB
testcase_29 AC 26 ms
6,456 KB
testcase_30 AC 40 ms
9,744 KB
testcase_31 AC 30 ms
7,352 KB
testcase_32 AC 150 ms
22,976 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 #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
template <class T> using V=vector<T>;
template <class T> using VV=V<V<T>>; //B(n,V<int>(n))
typedef pair<ll,ll> pll;

int dh[]={1,0,-1,0};
int dw[]={0,1,0,-1};
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  ll h,w;
  cin>>h>>w;
  VV<ll> A(h,V<ll>(w));
  V<ll> used(h*w),color(h*w,-1);
  rep(i,h) rep(j,w) cin>>A[i][j];
  used[0]=1,used[(h-1)*w+w-1]=1;
  color[0]=1,color[(h-1)*w+w-1]=0;
  priority_queue<pll,vector<pll>,greater<pll>> p0,p1;
  p1.push({A[0][1],1}),p1.push({A[1][0],w});
  p0.push({A[h-2][w-1],(h-2)*w+w-1}),p0.push({A[h-1][w-2],(h-1)*w+w-2});
  
  int ans=0;
  bool c=false;
  while(1){
    bool b=false;
    if(ans%2==0){
      while(p1.size()){
        auto a=p1.top(); p1.pop();
        if(used[a.second]) continue;
        b=true;
        used[a.second]=1;
        color[a.second]=1;
        int i=a.second/w,j=a.second%w;
        rep(k,4){
          int ni=i+dh[k],nj=j+dw[k];
          if(ni<0 || nj<0 || ni>=h || nj>=w) continue;
          if(used[ni*w+nj] && color[ni*w+nj]==0){
            c=true;
            break;
          }
          if(used[ni*w+nj]) continue;
          p1.push({A[ni][nj],ni*w+nj});
        }
        ans++;
        break;
      }
      if(c) break;
      if(b) continue;
      else break;
    }
    else{
      while(p1.size()){
        auto a=p0.top(); p0.pop();
        if(used[a.second]) continue;
        b=true;
        used[a.second]=1;
        color[a.second]=0;
        int i=a.second/w,j=a.second%w;
        rep(k,4){
          int ni=i+dh[k],nj=j+dw[k];
          if(ni<0 || nj<0 || ni>=h || nj>=w) continue;
          if(used[ni*w+nj] && color[ni*w+nj]==1){
            c=true;
            break;
          }
          if(used[ni*w+nj]) continue;
          p0.push({A[ni][nj],ni*w+nj});
        }
        ans++;
        break;
      }
      if(c) break;
      if(b) continue;
      else break;
    }
  }
  cout<<ans<<endl;
    
  return 0;
}
0