結果

問題 No.971 いたずらっ子
ユーザー simansiman
提出日時 2021-12-04 13:28:13
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,643 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 103,024 KB
実行使用メモリ 12,104 KB
最終ジャッジ日時 2023-09-20 23:56:55
合計ジャッジ時間 20,290 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 WA -
testcase_03 AC 1,685 ms
11,576 KB
testcase_04 AC 1,580 ms
11,124 KB
testcase_05 AC 1,705 ms
11,560 KB
testcase_06 AC 1,319 ms
9,684 KB
testcase_07 WA -
testcase_08 AC 429 ms
5,748 KB
testcase_09 AC 405 ms
5,552 KB
testcase_10 AC 10 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 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;

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

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

  Node(int y = -1, int x = -1, int dist = -1, int k_cnt = 0, ll cost = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
    this->k_cnt = k_cnt;
    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, 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 < 4; ++direct) {
      int ny = node.y + DY[direct];
      int nx = node.x + DX[direct];
      if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;

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

  return 0;
}
0