結果

問題 No.2731 Two Colors
ユーザー haihamabossu
提出日時 2024-04-19 21:52:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 256 ms / 3,000 ms
コード長 1,363 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 205,892 KB
最終ジャッジ日時 2025-02-21 04:10:37
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const pair<ll, ll> DIJ[4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
ll h, w, a[1010][1010], c[1010][1010];
priority_queue<tuple<ll, ll, ll>> que[2];
void solve() {
  cin >> h >> w;
  rep(i, h) rep(j, w) {
    cin >> a[i][j];
    a[i][j] *= -1;
    c[i][j] = -1;
  }
  c[0][0] = 1;
  c[h - 1][w - 1] = 0;
  que[1].emplace(a[0][1], 0, 1);
  que[1].emplace(a[1][0], 1, 0);
  que[0].emplace(a[h - 2][w - 1], h - 2, w - 1);
  que[0].emplace(a[h - 1][w - 2], h - 1, w - 2);
  ll turn = 0;
  for (;;) {
    turn += 1;
    ll va = 0, ci = 0, cj = 0;
    for (;;) {
      tie(va, ci, cj) = que[turn % 2].top();
      que[turn % 2].pop();
      if (c[ci][cj] == -1) {
        c[ci][cj] = turn % 2;
        break;
      }
    }
    for (const auto &[di, dj] : DIJ) {
      ll ni = ci + di;
      ll nj = cj + dj;
      if (ni < 0 || h <= ni || nj < 0 || w <= nj)
        continue;
      if (c[ni][nj] == -1) {
        que[turn % 2].emplace(a[ni][nj], ni, nj);
      } else if (c[ni][nj] == turn % 2) {
        continue;
      } else {
        cout << turn << '\n';
        return;
      }
    }
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0