結果

問題 No.402 最も海から遠い場所
ユーザー AquariusAquarius
提出日時 2019-10-15 16:16:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,374 ms / 3,000 ms
コード長 1,372 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 174,260 KB
実行使用メモリ 483,836 KB
最終ジャッジ日時 2023-08-28 15:01:00
合計ジャッジ時間 8,126 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 8 ms
5,420 KB
testcase_14 AC 4 ms
4,936 KB
testcase_15 AC 34 ms
13,292 KB
testcase_16 AC 45 ms
16,504 KB
testcase_17 AC 585 ms
132,508 KB
testcase_18 AC 1,075 ms
63,268 KB
testcase_19 AC 1,374 ms
483,836 KB
testcase_20 AC 952 ms
39,332 KB
testcase_21 AC 1,134 ms
264,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const short inf = 3e4;

using PP = pair<short, short>;
int h, w;
short a[3002][3002];
string s[3002];
int main() {
  cin >> h >> w;
  s[0] = string(w + 2, '.');
  s[h + 1] = string(w + 2, '.');
  for (int i = 0; i < h; ++i) {
    string t;
    cin >> t;
    s[i + 1] = '.' + t + '.';
  }
  
  for (int i = 0; i < h + 2; ++i) {
    for (int j = 0; j < w + 2; ++j) {
      a[i][j] = inf;
    }
  }
  
  queue<pair<short, PP>> q;
  for (int i = 0; i < h + 2; ++i) {
    for (int j = 0; j < w + 2; ++j) {
      if (i == 0 || i == h + 1
       || j == 0 || j == w + 1
       || s[i][j] == '.') {
        q.push(make_pair(0, PP(i, j)));
      }
    }
  }
  
  const int di[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
  const int dj[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
  while (!q.empty()) {
    auto p = q.front(); q.pop();
    short d = p.first;
    int i = p.second.first;
    int j = p.second.second;
    if (a[i][j] != inf) continue;
    a[i][j] = d;
    
    for (int k = 0; k < 8; ++k) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (ni < 0 || ni >= h + 2) continue;
      if (nj < 0 || nj >= w + 2) continue;
      q.push(make_pair(d + 1, PP(ni, nj)));
    }
  }
  
  short mx = 0;
  for (int i = 0; i < h + 2; ++i) {
    for (int j = 0; j < w + 2; ++j) {
      mx = max(mx, a[i][j]);
    }
  }
  cout << mx << endl;
}
0