結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2024-04-19 23:08:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 481 ms / 3,000 ms |
| コード長 | 2,323 bytes |
| コンパイル時間 | 4,417 ms |
| コンパイル使用メモリ | 255,448 KB |
| 最終ジャッジ日時 | 2025-02-21 05:23:37 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;
ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}
int main(){
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w));
cin >> a;
using PQ = priority_queue<P, vector<P>, greater<P>>;
PQ pq0;
PQ pq1;
pq0.emplace(0, 0);
pq1.emplace(0, h * w - 1);
int ans = -2;
bool stop_flag = false;
vector<int> dy = {1, 0, -1, 0};
vector<int> dx = {0, 1, 0, -1};
vector<vector<int>> col(h, vector<int>(w, -1));
auto paint = [&](PQ& pq, int c){
int tile = -1;
int fy = -1;
int fx = -1;
while(true){
auto p = pq.top(); pq.pop();
tile = p.second;
fy = tile / w;
fx = tile % w;
if(col[fy][fx] == -1) break;
}
col[fy][fx] = c;
rep(t, 4){
int ty = fy + dy[t];
int tx = fx + dx[t];
if(ty >= 0 and ty < h and tx >= 0 and tx < w){
if(col[ty][tx] == -1){
pq.emplace(a[ty][tx], ty * w + tx);
}else{
if(col[ty][tx] != c) stop_flag = true;
}
}
}
ans++;
};
while(true){
paint(pq0, 0);
if(stop_flag) break;
paint(pq1, 1);
if(stop_flag) break;
}
cout << ans << "\n";
return 0;
}