結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー simansiman
提出日時 2022-05-31 15:33:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 3,000 ms
コード長 1,731 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 132,884 KB
実行使用メモリ 13,820 KB
最終ジャッジ日時 2023-10-21 00:38:51
合計ジャッジ時間 4,906 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 120 ms
5,716 KB
testcase_08 AC 112 ms
5,664 KB
testcase_09 AC 175 ms
13,820 KB
testcase_10 AC 176 ms
13,820 KB
testcase_11 AC 170 ms
13,804 KB
testcase_12 AC 143 ms
13,820 KB
testcase_13 AC 96 ms
13,812 KB
testcase_14 AC 111 ms
9,736 KB
testcase_15 AC 110 ms
9,736 KB
testcase_16 AC 47 ms
5,664 KB
testcase_17 AC 185 ms
9,736 KB
testcase_18 AC 47 ms
5,664 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 136 ms
9,640 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 67 ms
9,736 KB
testcase_25 AC 128 ms
5,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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