結果

問題 No.1949 足し算するだけのパズルゲーム(2)
コンテスト
ユーザー siman
提出日時 2022-05-31 15:33:09
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 145 ms / 3,000 ms
コード長 1,731 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,936 ms
コンパイル使用メモリ 150,656 KB
実行使用メモリ 13,932 KB
最終ジャッジ日時 2026-04-09 09:26:31
合計ジャッジ時間 8,513 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:40:11: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   40 |   ll A[H][W];
      |           ^
main.cpp:40:11: note: read of non-const variable 'W' is not allowed in a constant expression
main.cpp:36:10: note: declared here
   36 |   int H, W, Y, X;
      |          ^
main.cpp:40:8: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   40 |   ll A[H][W];
      |        ^
main.cpp:40:8: note: read of non-const variable 'H' is not allowed in a constant expression
main.cpp:36:7: note: declared here
   36 |   int H, W, Y, X;
      |       ^
main.cpp:57:19: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   57 |   bool visited[H][W];
      |                   ^
main.cpp:57:19: note: read of non-const variable 'W' is not allowed in a constant expression
main.cpp:36:10: note: declared here
   36 |   int H, W, Y, X;
      |          ^
main.cpp:57:16: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   57 |   bool visited[H][W];
      |                ^
main.cpp:57:16: note: read of non-const variable 'H' is not allowed in a constant expression
main.cpp:36:7: note: declared here
   36 |   int H, W, Y, X;
      |       ^
4 warnings generated.

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

struct Node {
  int y;
  int x;
  ll value;

  Node(int y = -1, int x = -1, ll value = 0) {
    this->y = y;
    this->x = x;
    this->value = value;
  }

  bool operator>(const Node &n) const {
    return value > n.value;
  }
};

int main() {
  int H, W, Y, X;
  cin >> H >> W >> Y >> X;
  priority_queue <Node, vector<Node>, greater<Node>> pque;

  ll A[H][W];
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      cin >> A[y][x];
    }
  }

  for (int dir = 0; dir < 4; ++dir) {
    int ny = (Y - 1) + DY[dir];
    int nx = (X - 1) + DX[dir];
    if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;

    pque.push(Node(ny, nx, A[ny][nx]));
  }

  ll power = A[Y - 1][X - 1];

  bool visited[H][W];
  memset(visited, false, sizeof(visited));
  visited[Y - 1][X - 1] = true;
  int cnt = 0;

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (power <= node.value) continue;
    if (visited[node.y][node.x]) continue;
    visited[node.y][node.x] = true;

    power += node.value;
    ++cnt;
    // fprintf(stderr, "power: %lld\n", power);

    for (int dir = 0; dir < 4; ++dir) {
      int ny = node.y + DY[dir];
      int nx = node.x + DX[dir];
      if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;

      pque.push(Node(ny, nx, A[ny][nx]));
    }
  }

  if (cnt == H * W - 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
0