結果
問題 | No.2731 Two Colors |
ユーザー |
![]() |
提出日時 | 2024-04-19 22:05:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 464 ms / 3,000 ms |
コード長 | 1,735 bytes |
コンパイル時間 | 2,550 ms |
コンパイル使用メモリ | 214,932 KB |
最終ジャッジ日時 | 2025-02-21 04:19:36 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>using namespace std;vector<int> di = {-1, 0, 1, 0};vector<int> dj = {0, 1, 0, -1};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];}}auto inc = [&](int i, int j) {return (0 <= i && i < h && 0 <= j && j < w);};vector<vector<int>> b(h, vector<int>(w, -1));vector<vector<vector<bool>>> visit(2, vector<vector<bool>>(h, vector<bool>(w, false)));b[0][0] = 1;visit[1][0][0] = true;b[h - 1][w - 1] = 0;visit[0][h - 1][w - 1] = true;vector<priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>>> pq(2);pq[1].push({a[0][1], 0, 1});visit[1][0][1] = true;pq[1].push({a[1][0], 1, 0});visit[1][1][0] = true;pq[0].push({a[h - 1][w - 2], h - 1, w - 2});visit[0][h - 1][w - 2] = true;pq[0].push({a[h - 2][w - 1], h - 2, w - 1});visit[0][h - 2][w - 1] = true;int cnt = 0;while (true) {cnt++;int id = cnt % 2;auto [x, i, j] = pq[id].top();pq[id].pop();b[i][j] = id;bool ok = true;for (int k = 0; k < 4; k++) {int ni = i + di[k], nj = j + dj[k];if (!inc(ni, nj)) {continue;}if (b[ni][nj] == 1 - id) {ok = false;} else if (b[ni][nj] == -1 && !visit[id][ni][nj]) {visit[id][ni][nj] = true;pq[id].push({a[ni][nj], ni, nj});}}if (!ok) {break;}}cout << cnt << endl;}