結果

問題 No.402 最も海から遠い場所
ユーザー simansiman
提出日時 2021-10-08 01:30:35
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,777 bytes
コンパイル時間 4,957 ms
コンパイル使用メモリ 107,740 KB
実行使用メモリ 348,136 KB
最終ジャッジ日時 2023-09-30 09:11:09
合計ジャッジ時間 12,464 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 19 ms
6,164 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 100 ms
14,636 KB
testcase_16 AC 148 ms
15,216 KB
testcase_17 WA -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#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 by;
  int bx;
  int y;
  int x;
  int dist;

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

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

int main() {
  int H, W;
  cin >> H >> W;
  priority_queue <Node, vector<Node>, greater<Node>> pque;
  int max_dist[H + 2][W + 2];
  memset(max_dist, -1, sizeof(max_dist));
  int ans = 0;

  for (int y = 1; y <= H; ++y) {
    string row;
    cin >> row;
    for (int x = 1; x <= W; ++x) {
      if (row[x - 1] == '.') {
        pque.push(Node(y, x, y, x, 0));
      }
    }
  }

  for (int y = 0; y < H + 2; ++y) {
    for (int x = 0; x < W + 2; ++x) {
      if (y == 0 || x == 0 || y == H + 1 || x == W + 1) {
        pque.push(Node(y, x, y, x, 0));
      }
    }
  }

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

    if (max_dist[node.y][node.x] != -1 && max_dist[node.y][node.x] <= node.dist) continue;
    max_dist[node.y][node.x] = node.dist;
    ans = max(ans, node.dist);

    for (int direct = 0; direct < 4; ++direct) {
      int ny = node.y + DY[direct];
      int nx = node.x + DX[direct];
      if (ny < 0 || H <= ny || nx < 0 || W <= nx) continue;
      int nd = max(abs(ny - node.by), abs(nx - node.bx));

      pque.push(Node(node.by, node.bx, ny, nx, nd));
    }
  }

  cout << ans << endl;

  return 0;
}
0