結果
問題 | No.2731 Two Colors |
ユーザー |
![]() |
提出日時 | 2024-04-19 22:02:34 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 459 ms / 3,000 ms |
コード長 | 3,043 bytes |
コンパイル時間 | 5,534 ms |
コンパイル使用メモリ | 320,492 KB |
実行使用メモリ | 19,984 KB |
最終ジャッジ日時 | 2024-10-11 15:12:11 |
合計ジャッジ時間 | 12,763 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;typedef long long ll;typedef long double ld;#define int ll#define OVERLOAD_REP(_1, _2, _3, name, ...) name#define REP2(i, l, r) for (int i = (int)(l); i < (int)(r); i++)#define REP1(i, n) REP2(i, 0, n)#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)#define RREP2(i, l, r) for (int i = (int)(l)-1; i >= (int)(r); i--)#define RREP1(i, n) RREP2(i, n, 0)#define rrep(...) OVERLOAD_REP(__VA_ARGS__, RREP2, RREP1)(__VA_ARGS__)#define all(...) begin(__VA_ARGS__), end(__VA_ARGS__)#define rall(...) rbegin(__VA_ARGS__), rend(__VA_ARGS__)#define debug(x) cerr << #x << ": " << (x) << endl#define pb push_back#define mp make_pair#define mt make_tupleconst int INF = 4e18;const int MOD = 1e9 + 7;const int MOD2 = 998244353;template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }template<class... T> void input(T&... args) { ((cin >> args), ...); }template<class... T> void print(T... args) { ((cout << args << " "), ...); }template<class... T> void println(T... args) { print(args...); cout << endl; }signed main() {int H, W;input(H, W);vector<vector<int>> A(H, vector<int>(W));rep(i, H) rep(j, W) input(A[i][j]);vector<vector<int>> B(H, vector<int>(W, -1));B[0][0] = 1;B[H-1][W-1] = 0;priority_queue<pair<int, pair<int, int>>,vector<pair<int, pair<int, int>>>,greater<pair<int, pair<int, int>>>> color0_next;color0_next.push(mp(A[H-2][W-1], mp(H-2, W-1)));color0_next.push(mp(A[H-1][W-2], mp(H-1, W-2)));priority_queue<pair<int, pair<int, int>>,vector<pair<int, pair<int, int>>>,greater<pair<int, pair<int, int>>>> color1_next;color1_next.push(mp(A[1][0], mp(1, 0)));color1_next.push(mp(A[0][1], mp(0, 1)));int i = 1;while (true) {if (i % 2 == 0) {while (!color0_next.empty()) {auto [di, dj] = color0_next.top().second;color0_next.pop();if (B[di][dj] == -1) {B[di][dj] = 0;for (auto [ddi, ddj] : {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)}) {ddi = max(0LL, min(H-1, di + ddi));ddj = max(0LL, min(W-1, dj + ddj));if (B[ddi][ddj] == 1) {println(i);return 0;} else if (B[ddi][ddj] == -1) {color0_next.push(mp(A[ddi][ddj], mp(ddi, ddj)));}}break;}}} else {while (!color1_next.empty()) {auto [di, dj] = color1_next.top().second;color1_next.pop();if (B[di][dj] == -1) {B[di][dj] = 1;for (auto [ddi, ddj] : {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)}) {ddi = max(0LL, min(H-1, di + ddi));ddj = max(0LL, min(W-1, dj + ddj));if (B[ddi][ddj] == 0) {println(i);return 0;} else if (B[ddi][ddj] == -1) {color1_next.push(mp(A[ddi][ddj], mp(ddi, ddj)));}}break;}}}++i;}}