結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-19 22:04:37 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 452 ms / 3,000 ms |
| コード長 | 2,581 bytes |
| コンパイル時間 | 1,309 ms |
| コンパイル使用メモリ | 106,520 KB |
| 実行使用メモリ | 9,948 KB |
| 最終ジャッジ日時 | 2024-10-11 15:14:18 |
| 合計ジャッジ時間 | 8,544 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
constexpr int iINF = 1'000'000'000;
int main () {
int H, W; cin >> H >> W;
vector<vector<int>> A(H, vector<int>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) cin >> A[i][j];
}
// グリッド上のダイクストラ法に帰着
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq1, pq2;
vector<vector<bool>> vis1(H, vector<bool>(W, false)), vis2(H, vector<bool>(W, false));
pq1.push(make_pair(A[0][0], make_pair(0, 0)));
pq2.push(make_pair(A[H - 1][W - 1], make_pair(H - 1, W - 1)));
const vector<pair<int, int>> dxy = {
make_pair(0, 1),
make_pair(0, -1),
make_pair(1, 0),
make_pair(-1, 0),
};
bool end = false;
int ans = 0;
while (true) {
{
int y, x, c;
while (true) {
auto h = pq1.top(); pq1.pop();
c = h.first;
y = h.second.first, x = h.second.second;
if (vis1[y][x]) continue;
break;
}
vis1[y][x] = true;
ans++;
for (auto d : dxy) {
int ny = y + d.first, nx = x + d.second;
if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
if (vis1[ny][nx]) continue;
// 相手側がもう塗られている
if (vis2[ny][nx]) {
end = true;
break;
}
pq1.push(make_pair(A[ny][nx], make_pair(ny, nx)));
}
}
if (end) break;
{
int y, x, c;
while (true) {
auto h = pq2.top(); pq2.pop();
c = h.first;
y = h.second.first, x = h.second.second;
if (vis2[y][x]) continue;
break;
}
vis2[y][x] = true;
ans++;
for (auto d : dxy) {
int ny = y + d.first, nx = x + d.second;
if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
if (vis2[ny][nx]) continue;
// 相手側がもう塗られている
if (vis1[ny][nx]) {
end = true;
break;
}
pq2.push(make_pair(A[ny][nx], make_pair(ny, nx)));
}
}
if (end) break;
}
cout << ans - 2 << "\n";
}