結果

問題 No.402 最も海から遠い場所
ユーザー k
提出日時 2021-04-16 01:56:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 412 ms / 3,000 ms
コード長 1,201 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 208,712 KB
最終ジャッジ日時 2025-01-20 18:03:57
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e6;

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

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

  W += 2;
  vector<string> bd;
  bd.push_back(string(W, '.'));
  for (int i = 0; i < H; i++) {
    string tmp;
    cin >> tmp;
    tmp = "." + tmp + ".";
    bd.push_back(tmp);
  }
  bd.push_back(string(W, '.'));
  H += 2;


  vector<vector<int> > dist(H, vector<int>(W, INF));
  queue<pair<int, int> > q;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (bd[i][j] == '.') {
        q.emplace(i, j);
        dist[i][j] = 0;
      }
    }
  }

  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();

    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        int yt = y + i;
        int xt = x + j;

        if (xt < 0 || xt >= W) continue;
        if (yt < 0 || yt >= H) continue;
        if (dist[yt][xt] == INF) {
          q.emplace(yt, xt);
          dist[yt][xt] = dist[y][x] + 1;
        }
      }
    }
  }
  
  int ret = 0;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      ret = max(ret, dist[i][j]);

  cout << ret << endl;
  
  return 0;
}
0