結果

問題 No.402 最も海から遠い場所
ユーザー ei1333333ei1333333
提出日時 2016-07-22 22:47:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 458 ms / 3,000 ms
コード長 2,629 bytes
コンパイル時間 1,462 ms
コンパイル使用メモリ 164,828 KB
実行使用メモリ 56,488 KB
最終ジャッジ日時 2024-11-06 12:51:20
合計ジャッジ時間 4,724 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
38,808 KB
testcase_01 AC 15 ms
38,788 KB
testcase_02 AC 14 ms
38,724 KB
testcase_03 AC 15 ms
38,752 KB
testcase_04 AC 14 ms
38,844 KB
testcase_05 AC 14 ms
38,876 KB
testcase_06 AC 15 ms
38,768 KB
testcase_07 AC 15 ms
38,792 KB
testcase_08 AC 14 ms
38,784 KB
testcase_09 AC 14 ms
38,784 KB
testcase_10 AC 14 ms
38,784 KB
testcase_11 AC 14 ms
38,828 KB
testcase_12 AC 15 ms
38,772 KB
testcase_13 AC 17 ms
38,776 KB
testcase_14 AC 15 ms
38,784 KB
testcase_15 AC 30 ms
39,248 KB
testcase_16 AC 32 ms
39,476 KB
testcase_17 AC 292 ms
47,260 KB
testcase_18 AC 458 ms
56,364 KB
testcase_19 AC 243 ms
56,460 KB
testcase_20 AC 456 ms
56,488 KB
testcase_21 AC 304 ms
56,440 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