結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
FplusFplusF
|
| 提出日時 | 2024-04-05 11:44:07 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 280 ms / 3,000 ms |
| コード長 | 1,878 bytes |
| コンパイル時間 | 2,751 ms |
| コンパイル使用メモリ | 263,976 KB |
| 実行使用メモリ | 21,248 KB |
| 最終ジャッジ日時 | 2024-10-03 13:28:23 |
| 合計ジャッジ時間 | 7,545 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#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;
}
}
FplusFplusF