結果

問題 No.402 最も海から遠い場所
ユーザー ei1333333ei1333333
提出日時 2016-07-22 22:47:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 443 ms / 3,000 ms
コード長 2,629 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 149,440 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2023-08-06 09:52:32
合計ジャッジ時間 4,637 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
38,708 KB
testcase_01 AC 14 ms
38,680 KB
testcase_02 AC 14 ms
38,772 KB
testcase_03 AC 14 ms
38,944 KB
testcase_04 AC 14 ms
38,708 KB
testcase_05 AC 14 ms
38,752 KB
testcase_06 AC 14 ms
38,708 KB
testcase_07 AC 15 ms
38,688 KB
testcase_08 AC 14 ms
38,680 KB
testcase_09 AC 14 ms
38,660 KB
testcase_10 AC 14 ms
38,844 KB
testcase_11 AC 14 ms
38,672 KB
testcase_12 AC 15 ms
38,668 KB
testcase_13 AC 17 ms
39,080 KB
testcase_14 AC 15 ms
38,732 KB
testcase_15 AC 29 ms
39,136 KB
testcase_16 AC 31 ms
39,336 KB
testcase_17 AC 287 ms
47,256 KB
testcase_18 AC 441 ms
56,344 KB
testcase_19 AC 236 ms
56,308 KB
testcase_20 AC 443 ms
56,400 KB
testcase_21 AC 294 ms
56,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int H, W;
string S[3003];
int dist[3004][3004];

int main()
{

  cin >> H >> W;
  for(int i = 1; i <= H; i++) {
    cin >> S[i];
    S[i] = "." + S[i] + ".";
  }
  S[0] = S[H + 1] = string(W + 2, '.');

  int ret = 0;
  fill_n(*dist, 3004 * 3004, INF);
  for(int i = 0; i <= H + 1; i++) {
    for(int j = 0; j <= W + 1; j++) {
      if(S[i][j] == '.') dist[i][j] = 0;
    }
  }
  for(int i = 1; i <= H; i++) {
    for(int j = 1; j <= W; j++) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j + 1] + 1);
      }
    }
    
  }
  for(int i = H; i > 0; i--) {
    for(int j = W; j > 0; j--) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j - 1] + 1);
      }
    }
  }
    for(int i = 1; i <= H; i++) {
    for(int j = 1; j <= W; j++) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j + 1] + 1);
      }
    }
    
  }
  for(int i = H; i > 0; i--) {
    for(int j = W; j > 0; j--) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j - 1] + 1);
      }
    }
  }
    for(int i = 1; i <= H; i++) {
    for(int j = 1; j <= W; j++) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j - 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i - 1][j + 1] + 1);
      }
    }
    
  }
  for(int i = H; i > 0; i--) {
    for(int j = W; j > 0; j--) {
      if(S[i][j] == '#') {
        dist[i][j] = min(dist[i][j], dist[i][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j + 1] + 1);
        dist[i][j] = min(dist[i][j], dist[i + 1][j - 1] + 1);
        ret = max(ret, dist[i][j]);
      }
    }
  }
  cout << ret << endl;
}
0