結果
問題 |
No.2731 Two Colors
|
ユーザー |
|
提出日時 | 2024-04-19 21:41:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 300 ms / 3,000 ms |
コード長 | 2,123 bytes |
コンパイル時間 | 2,219 ms |
コンパイル使用メモリ | 208,676 KB |
最終ジャッジ日時 | 2025-02-21 03:57:23 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int H,W; cin>>H>>W; vector A(H,vector<int>(W)); for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin>>A[i][j]; } } set<tuple<int,int,int>>s0,s1; s0.insert(make_tuple(A[0][1],0,1)); s0.insert(make_tuple(A[1][0],1,0)); s1.insert(make_tuple(A[H-1][W-2],H-1,W-2)); s1.insert(make_tuple(A[H-2][W-1],H-2,W-1)); vector F(H,vector<int>(W,-1)); F[0][0]=0; F[H-1][W-1]=1; for(int t=0;;t++){ if(t%2==0){ if(s0.empty()){ cout<<t<<"\n"; return 0; } auto[c,x,y]=*s0.begin(); s0.erase(make_tuple(c,x,y)); F[x][y]=0; for(int i=0;i<4;i++){ int ex=x+dx[i],ey=y+dy[i]; if(0<=ex&&ex<H&&0<=ey&&ey<W&&F[ex][ey]==-1){ s0.insert(make_tuple(A[ex][ey],ex,ey)); } } if(s1.count(make_tuple(c,x,y))){ cout<<t+1<<"\n"; return 0; } }else{ if(s1.empty()){ cout<<t<<"\n"; return 0; } auto[c,x,y]=*s1.begin(); s1.erase(make_tuple(c,x,y)); F[x][y]=1; for(int i=0;i<4;i++){ int ex=x+dx[i],ey=y+dy[i]; if(0<=ex&&ex<H&&0<=ey&&ey<W&&F[ex][ey]==-1){ s1.insert(make_tuple(A[ex][ey],ex,ey)); } } if(s0.count(make_tuple(c,x,y))){ cout<<t+1<<"\n"; return 0; } } } }