結果
| 問題 | No.2731 Two Colors |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-19 22:00:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 232 ms / 3,000 ms |
| コード長 | 2,065 bytes |
| コンパイル時間 | 2,622 ms |
| コンパイル使用メモリ | 200,456 KB |
| 最終ジャッジ日時 | 2025-02-21 04:16:47 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
#define INF 1000000001LL
#define LNF 1000000000000000001LL
#define MOD 998244353LL
#define MAX 1005
#define long long long
#define all(x) x.begin(),x.end()
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
int pan[n][m];
for(int i = 0; i<n; i++)
{
for(int j = 0;j<m; j++)
cin >> pan[i][j];
}
int dx[4] = {-1,0,0,1};
int dy[4] = {0,-1,1,0};
int vis[n][m];
memset(vis,0,sizeof vis);
priority_queue<pair<int,pair<int,int>>> pqA;
priority_queue<pair<int,pair<int,int>>> pqB;
pqA.push({pan[0][0],{0,0}});
pqB.push({pan[n-1][m-1],{n-1,m-1}});
int cnt = -2;
while(true)
{
pair<int,int> x = pqA.top().second;
pqA.pop();
while(vis[x.first][x.second])
{
x = pqA.top().second;
pqA.pop();
}
vis[x.first][x.second] = 1;
cnt++;
for(int i = 0; i<4; i++)
{
int xx = x.first+dx[i];
int yy = x.second+dy[i];
if(xx < 0 || yy < 0 || xx>=n || yy >=m)
continue;
if(vis[xx][yy] == 2)
{
cout << cnt << endl;
return 0;
}
if(vis[xx][yy])
continue;
pqA.push({-pan[xx][yy],{xx,yy}});
}
x = pqB.top().second;
pqB.pop();
while(vis[x.first][x.second])
{
x = pqB.top().second;
pqB.pop();
}
vis[x.first][x.second] = 2;
cnt++;
for(int i = 0; i<4; i++)
{
int xx = x.first+dx[i];
int yy = x.second+dy[i];
if(xx < 0 || yy < 0 || xx>=n || yy >=m)
continue;
if(vis[xx][yy] == 1)
{
cout << cnt << endl;
return 0;
}
if(vis[xx][yy])
continue;
pqB.push({-pan[xx][yy],{xx,yy}});
}
}
return 0;
}