結果

問題 No.402 最も海から遠い場所
ユーザー kusaf_kusaf_
提出日時 2023-12-08 02:28:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 629 ms / 3,000 ms
コード長 2,133 bytes
コンパイル時間 3,310 ms
コンパイル使用メモリ 262,172 KB
実行使用メモリ 263,892 KB
最終ジャッジ日時 2023-12-08 02:28:08
合計ジャッジ時間 7,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 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 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 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 32 ms
9,984 KB
testcase_16 AC 24 ms
8,960 KB
testcase_17 AC 566 ms
129,632 KB
testcase_18 AC 489 ms
45,440 KB
testcase_19 AC 321 ms
38,656 KB
testcase_20 AC 495 ms
39,296 KB
testcase_21 AC 629 ms
263,892 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};

struct Time {
  int lim;
  bool start;
  chrono::system_clock::time_point t;
  Time(int lim): lim(lim), start(false){};
  void operator()() {
    if(!start) {
      t = chrono::system_clock::now();
      start = true;
      return;
    }
    auto d = chrono::system_clock::now() - t;
    auto msec = chrono::duration_cast<chrono::milliseconds>(d).count();
    cerr << "\n-------------------------------\n";
    if(msec <= lim) {
      cerr << "[\033[36mi\033[m] ";
      cerr << "\033[36mExecution Time : " << msec << " ms\033[m\n";
    }
    else {
      cerr << "[\033[33m!\033[m] ";
      cerr << "\033[33mExecution Time : " << msec << " ms\033[m\n";
    }
    cerr << "-------------------------------\n";
  }
} Time(3000);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Time();
  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";
  Time();
}
0