結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2024-04-19 22:53:11 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 244 ms / 3,000 ms |
| コード長 | 2,076 bytes |
| コンパイル時間 | 3,696 ms |
| コンパイル使用メモリ | 256,096 KB |
| 実行使用メモリ | 26,752 KB |
| 最終ジャッジ日時 | 2024-10-11 16:48:43 |
| 合計ジャッジ時間 | 7,426 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#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;
}
umezo