結果

問題 No.402 最も海から遠い場所
ユーザー kusaf_kusaf_
提出日時 2023-12-08 02:25:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 604 ms / 3,000 ms
コード長 1,385 bytes
コンパイル時間 3,114 ms
コンパイル使用メモリ 261,700 KB
実行使用メモリ 263,764 KB
最終ジャッジ日時 2023-12-08 02:26:04
合計ジャッジ時間 7,584 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 1 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 1 ms
6,548 KB
testcase_10 AC 1 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 7 ms
6,548 KB
testcase_14 AC 3 ms
6,548 KB
testcase_15 AC 31 ms
9,984 KB
testcase_16 AC 24 ms
8,832 KB
testcase_17 AC 562 ms
129,504 KB
testcase_18 AC 442 ms
45,312 KB
testcase_19 AC 308 ms
38,656 KB
testcase_20 AC 523 ms
39,296 KB
testcase_21 AC 604 ms
263,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;

  vector<vector<int>> d(H + 2, vector<int>(W + 2, 0));
  for(int i = 1; i <= H; i++) {
    for(int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if(c == '#') { d[i][j] = 1e9; }
    }
  }

  queue<tuple<int, int, int>> q;
  for(int i = 0; i < H + 2; i++) {
    for(int j = 0; j < W + 2; j++) {
      if(!d[i][j]) {
        for(int k = 0; k < 8; k++) {
          int ni = i + DX[k], nj = j + DY[k];
          if(ni < 0 || ni >= H + 2 || nj < 0 || nj >= W + 2) { continue; }
          if(!d[ni][nj]) { continue; }
          q.emplace(i, j, k);
        }
      }
    }
  }

  while(!q.empty()) {
    auto [i, j, k] = q.front();
    q.pop();
    int ni = i + DX[k], nj = j + DY[k];
    if(ni < 0 || ni >= H + 2 || nj < 0 || nj >= W + 2) { continue; }
    if(d[ni][nj] > d[i][j] + 1) {
      d[ni][nj] = d[i][j] + 1;
      q.emplace(ni, nj, k);
      if(k == 4 || k == 6) { q.emplace(ni, nj, 0); }
      if(k == 5 || k == 7) { q.emplace(ni, nj, 1); }
      if(k == 4 || k == 5) { q.emplace(ni, nj, 2); }
      if(k == 6 || k == 7) { q.emplace(ni, nj, 3); }
    }
  }

  int r = 0;
  for(auto &i : d) { r = max(r, ranges::max(i)); }
  cout << r << "\n";
}
0