結果

問題 No.2328 Build Walls
ユーザー simansiman
提出日時 2023-05-31 15:52:48
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 504 ms / 3,000 ms
コード長 1,533 bytes
コンパイル時間 3,102 ms
コンパイル使用メモリ 105,712 KB
実行使用メモリ 6,800 KB
最終ジャッジ日時 2023-08-28 01:32:51
合計ジャッジ時間 9,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 67 ms
5,140 KB
testcase_14 AC 159 ms
4,452 KB
testcase_15 AC 93 ms
4,380 KB
testcase_16 AC 15 ms
4,376 KB
testcase_17 AC 81 ms
4,588 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 58 ms
4,976 KB
testcase_22 AC 258 ms
4,848 KB
testcase_23 AC 372 ms
6,752 KB
testcase_24 AC 327 ms
6,728 KB
testcase_25 AC 364 ms
6,724 KB
testcase_26 AC 247 ms
6,716 KB
testcase_27 AC 308 ms
6,676 KB
testcase_28 AC 115 ms
6,596 KB
testcase_29 AC 371 ms
6,800 KB
testcase_30 AC 127 ms
6,612 KB
testcase_31 AC 125 ms
6,612 KB
testcase_32 AC 300 ms
6,680 KB
testcase_33 AC 504 ms
6,660 KB
testcase_34 AC 128 ms
6,604 KB
testcase_35 AC 375 ms
6,764 KB
testcase_36 AC 2 ms
4,376 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;

struct Node {
  int y;
  int x;
  int cost;

  Node(int y = -1, int x = 1, int cost = -1) {
    this->y = y;
    this->x = x;
    this->cost = cost;
  }

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

int main() {
  int H, W;
  cin >> H >> W;

  int A[H][W];
  memset(A, -1, sizeof(A));

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

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  for (int y = 1; y <= H - 2; ++y) {
    if (A[y][0] == -1) continue;

    pque.push(Node(y, 0, A[y][0]));
  }

  bool visited[H][W];
  memset(visited, false, sizeof(visited));

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

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

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

    if (node.x == W - 1) {
      cout << node.cost << endl;
      return 0;
    }

    for (int dir = 0; dir < 8; ++dir) {
      int ny = node.y + DY[dir];
      int nx = node.x + DX[dir];
      if (ny < 0 || nx < 0) continue;
      if (A[ny][nx] == -1) continue;

      int ncost = node.cost + A[ny][nx];
      pque.push(Node(ny, nx, ncost));
    }
  }

  cout << -1 << endl;

  return 0;
}
0