結果

問題 No.2731 Two Colors
ユーザー 👑 emthrmemthrm
提出日時 2024-04-19 21:39:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 3,000 ms
コード長 1,882 bytes
コンパイル時間 4,965 ms
コンパイル使用メモリ 290,108 KB
実行使用メモリ 11,532 KB
最終ジャッジ日時 2024-04-19 21:40:22
合計ジャッジ時間 8,213 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 235 ms
11,008 KB
testcase_01 AC 233 ms
11,136 KB
testcase_02 AC 234 ms
11,136 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 27 ms
5,376 KB
testcase_07 AC 29 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 100 ms
9,596 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 116 ms
11,532 KB
testcase_12 AC 101 ms
9,452 KB
testcase_13 AC 95 ms
9,144 KB
testcase_14 AC 50 ms
6,112 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 45 ms
5,952 KB
testcase_17 AC 62 ms
7,144 KB
testcase_18 AC 13 ms
5,376 KB
testcase_19 AC 48 ms
6,384 KB
testcase_20 AC 66 ms
7,488 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 111 ms
10,276 KB
testcase_23 AC 29 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 87 ms
8,568 KB
testcase_27 AC 110 ms
10,364 KB
testcase_28 AC 75 ms
8,312 KB
testcase_29 AC 24 ms
5,376 KB
testcase_30 AC 41 ms
6,408 KB
testcase_31 AC 28 ms
5,412 KB
testcase_32 AC 136 ms
11,496 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int h, w; cin >> h >> w;
  vector a(h, vector(w, 0));
  for (vector<int>& a_i : a) for (int& a_ij : a_i) cin >> a_ij;
  vector color(h, vector(w, -1));
  color[0][0] = 1;
  color[h - 1][w - 1] = 0;
  array<priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>>, 2> que{};
  que[0].emplace(a[h - 1][w - 2], h - 1, w - 2);
  que[0].emplace(a[h - 2][w - 1], h - 2, w - 1);
  que[1].emplace(a[0][1], 0, 1);
  que[1].emplace(a[1][0], 1, 0);
  for (int ans = 1; ; ++ans) {
    int y = -1, x = -1;
    while (true) {
      tie(ignore, y, x) = que[ans % 2].top();
      que[ans % 2].pop();
      if (color[y][x] == -1) break;
    }
    color[y][x] = ans % 2;
    REP(k, 4) {
      const int ny = y + DY4[k], nx = x + DX4[k];
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        if (color[ny][nx] == -1) {
          que[ans % 2].emplace(a[ny][nx], ny, nx);
        } else if (color[ny][nx] != color[y][x]) {
          cout << ans << '\n';
          return 0;
        }
      }
    }
  }
}
0