結果

問題 No.971 いたずらっ子
ユーザー simansiman
提出日時 2021-12-04 15:05:13
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 898 ms / 2,000 ms
コード長 1,584 bytes
コンパイル時間 1,509 ms
コンパイル使用メモリ 140,120 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-25 02:18:31
合計ジャッジ時間 9,474 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 898 ms
11,520 KB
testcase_01 AC 884 ms
11,392 KB
testcase_02 AC 649 ms
9,600 KB
testcase_03 AC 811 ms
11,520 KB
testcase_04 AC 762 ms
11,008 KB
testcase_05 AC 736 ms
11,392 KB
testcase_06 AC 562 ms
9,600 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 181 ms
5,504 KB
testcase_09 AC 173 ms
5,504 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,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;

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

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

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

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

int main() {
  int H, W;
  cin >> H >> W;
  char G[H][W];

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

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(0, 0, 0, 0));
  bool visited[H][W];
  memset(visited, false, sizeof(visited));

  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.y == H - 1 && node.x == W - 1) {
      cout << node.cost << endl;
      break;
    }

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

      if (G[ny][nx] == 'k') {
        pque.push(Node(ny, nx, node.dist + 1, node.cost + 1 + (node.dist + 1)));
      } else {
        pque.push(Node(ny, nx, node.dist + 1, node.cost + 1));
      }
    }
  }

  return 0;
}
0