結果

問題 No.2328 Build Walls
ユーザー siman
提出日時 2023-05-31 15:52:48
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 703 ms / 3,000 ms
コード長 1,533 bytes
コンパイル時間 4,316 ms
コンパイル使用メモリ 141,924 KB
実行使用メモリ 6,784 KB
最終ジャッジ日時 2024-12-28 13:51:05
合計ジャッジ時間 11,829 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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