結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-05 10:11:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 204 ms / 3,000 ms |
| コード長 | 1,309 bytes |
| コンパイル時間 | 1,970 ms |
| コンパイル使用メモリ | 206,868 KB |
| 最終ジャッジ日時 | 2025-02-21 10:58:18 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<vector<int>> A(h, vector<int>(w)), B(h, vector<int>(w, -1));
for(auto &&vec : A){
for(auto &&v : vec) cin >> v;
}
A[0][0] = A[h - 1][w - 1] = 1;
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> pq1, pq2;
pq1.emplace(0, 0, 0);
pq2.emplace(0, h - 1, w - 1);
B[0][0] = 0;
B[h - 1][w - 1] = 1;
for(int i = 0; i < h * w; i++){
int s = i % 2;
auto [v, y, x] = (s == 0 ? pq1.top() : pq2.top());
(s == 0 ? pq1.pop() : pq2.pop());
B[y][x] = s;
for(int j = 0; j < 4; j++){
int ny = y + (j == 0) - (j == 1);
int nx = x + (j == 2) - (j == 3);
if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
if(B[ny][nx] >= 0 && B[ny][nx] != s){
cout << i + 1 - 2 << '\n';
return 0;
}
if((-B[ny][nx] - 1) >> s & 1) continue;
B[ny][nx] -= 1 << s;
if(s == 0){
pq1.emplace(A[ny][nx], ny, nx);
}else{
pq2.emplace(A[ny][nx], ny, nx);
}
}
}
}