結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー a01sa01to
提出日時 2025-06-10 00:30:54
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,785 bytes
コンパイル時間 4,086 ms
コンパイル使用メモリ 294,400 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2025-06-10 00:31:03
合計ジャッジ時間 6,300 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  vector Grid(h, vector<ll>(w));
  rep(i, h) rep(j, w) cin >> Grid[i][j];
  constexpr ll INF = 1e18;
  vector power(2, vector(h, vector<ll>(w, -INF)));
  power[0][0][0] = Grid[0][0];
  priority_queue<tuple<ll, int, int, int>> pq;
  pq.emplace(power[0][0][0], 0, 0, 0);
  const auto inRange = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w;
  };
  while (!pq.empty()) {
    auto [p, flg, i, j] = pq.top();
    pq.pop();
    if (p < power[flg][i][j]) continue;
    if (i == h - 1 && j == w - 1) break;
    // D
    if (inRange(i + 1, j)) {
      if (Grid[i + 1][j] < p) {
        ll np = p + Grid[i + 1][j];
        if (np > power[flg][i + 1][j]) {
          power[flg][i + 1][j] = np;
          pq.push({ np, flg, i + 1, j });
        }
      }
      else if (flg == 0 && p > power[1][i + 1][j] && !(i + 1 == h - 1 && j == w - 1)) {
        power[1][i + 1][j] = p;
        pq.push({ p, 1, i + 1, j });
      }
    }
    // R
    if (inRange(i, j + 1)) {
      if (Grid[i][j + 1] < p) {
        ll np = p + Grid[i][j + 1];
        if (np > power[flg][i][j + 1]) {
          power[flg][i][j + 1] = np;
          pq.push({ np, flg, i, j + 1 });
        }
      }
      else if (flg == 0 && p > power[1][i][j + 1] && !(i == h - 1 && j + 1 == w - 1)) {
        power[1][i][j + 1] = p;
        pq.push({ p, 1, i, j + 1 });
      }
    }
  }
  cout << (max(power[0][h - 1][w - 1], power[1][h - 1][w - 1]) == -INF ? "No" : "Yes") << '\n';
  return 0;
}
0