結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-20 02:00:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 223 ms / 3,000 ms |
| コード長 | 1,761 bytes |
| コンパイル時間 | 2,311 ms |
| コンパイル使用メモリ | 209,884 KB |
| 最終ジャッジ日時 | 2025-02-21 06:00:25 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr int MOD=998244353;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H,W;
cin>>H>>W;
vector A(H,vector(W,0));
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
cin>>A[i][j];
}
}
vector used0(H,vector(W,false)),used1=used0;
vector colored(H,vector(W,0));
using a3i=array<int,3>;
priority_queue<a3i,vector<a3i>,greater<a3i>>pq0,pq1;
pq0.push({0,0,0});
used0[0][0]=1;
pq1.push({0,H-1,W-1});
used1[H-1][W-1]=1;
int ans=-2;
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
auto bound_ok=[&](int x,int y){
return x>=0&&x<H &&y>=0&&y<W;
};
while(1){
{
while(pq0.size()&&colored[pq0.top()[1]][pq0.top()[2]]){
pq0.pop();
}
if(pq0.empty())break;
auto [d,i,j]=pq0.top();
colored[i][j]=1;
ans+=1;
pq0.pop();
bool ng=0;
for(int k=0;k<4;k++){
int i2=i+dx[k],j2=j+dy[k];
if(bound_ok(i2,j2)&&colored[i2][j2]==2){
ng=1;
break;
}else if(bound_ok(i2,j2)&&!used0[i2][j2]){
used0[i2][j2]=1;
pq0.push({A[i2][j2],i2,j2});
}
}
if(ng)break;
}
{
while(pq1.size()&&colored[pq1.top()[1]][pq1.top()[2]]){
pq1.pop();
}
if(pq1.empty())break;
auto [d,i,j]=pq1.top();
colored[i][j]=2;
ans+=1;
pq1.pop();
bool ng=0;
for(int k=0;k<4;k++){
int i2=i+dx[k],j2=j+dy[k];
if(bound_ok(i2,j2)&&colored[i2][j2]==1){
ng=1;
break;
}else if(bound_ok(i2,j2)&&!used1[i2][j2]){
used1[i2][j2]=1;
pq1.push({A[i2][j2],i2,j2});
}
}
if(ng)break;
}
}
cout<<ans<<'\n';
}