結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー RVindicatio
提出日時 2022-05-20 22:27:18
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 129 ms / 3,000 ms
コード長 1,547 bytes
コンパイル時間 13,141 ms
コンパイル使用メモリ 301,112 KB
最終ジャッジ日時 2025-01-29 10:56:59
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = 1e9;
const ll inf = 1LL<<60;

template<typename T>
bool chmin(T& x, const T& y){
  if(x <= y) return false;
  x = y;
  return true;
}
template<typename T>
bool chmax(T& x, const T& y){
  if(x >= y) return false;
  x = y;
  return true;
}

void solve() {
  ll h, w, y, x; cin >> h >> w >> y >> x;
  y--; x--;
  vector<vector<ll>> a(h, vector<ll>(w));
  for (int i=0; i<h; i++) for (int j=0; j<w; j++) cin >> a[i][j];
  ll lv = a[y][x];
  priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>>> q;
  vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
  vector<vector<bool>> v(h, vector<bool>(w, false));
  v[y][x] = true;
  for (int k=0; k<4; k++) {
    int nx = y + dx[k], ny = x + dy[k];
    if (0 <= nx && nx < h && 0 <= ny && ny < w) {
      q.push({a[nx][ny], nx, ny});
      v[nx][ny] = true;
    }
  }
  while (q.size()) {
    auto now = q.top(); q.pop();
    int i = now[1], j = now[2];
    ll p = now[0];
    if (lv <= p) {
      cout << "No" << '\n';
      return;
    }
    lv += p;
    for (int k=0; k<4; k++) {
      int nx = i + dx[k], ny = j + dy[k];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && !v[nx][ny]) {
        v[nx][ny] = true;
        q.push({a[nx][ny], nx, ny});
      }
    }
  }
  cout << "Yes" << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  // int t; cin >> t;
  /*while (t--)*/ solve();
}
0