結果

問題 No.402 最も海から遠い場所
ユーザー takeya_okinotakeya_okino
提出日時 2017-07-07 12:34:47
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,012 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 78,008 KB
実行使用メモリ 397,796 KB
最終ジャッジ日時 2024-04-16 01:14:24
合計ジャッジ時間 17,462 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
41,312 KB
testcase_01 AC 139 ms
41,492 KB
testcase_02 AC 151 ms
41,660 KB
testcase_03 AC 138 ms
41,412 KB
testcase_04 AC 140 ms
41,344 KB
testcase_05 AC 139 ms
41,328 KB
testcase_06 AC 139 ms
41,304 KB
testcase_07 AC 125 ms
40,320 KB
testcase_08 AC 138 ms
41,340 KB
testcase_09 AC 139 ms
41,340 KB
testcase_10 AC 140 ms
41,364 KB
testcase_11 AC 146 ms
41,488 KB
testcase_12 AC 146 ms
41,540 KB
testcase_13 AC 216 ms
42,968 KB
testcase_14 AC 200 ms
42,876 KB
testcase_15 AC 355 ms
52,000 KB
testcase_16 AC 522 ms
60,360 KB
testcase_17 AC 1,510 ms
211,268 KB
testcase_18 TLE -
testcase_19 AC 1,059 ms
126,488 KB
testcase_20 AC 2,254 ms
144,440 KB
testcase_21 AC 2,396 ms
397,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int H = sc.nextInt();
    int W = sc.nextInt();
    char[][] board = new char[H][W];
    int[][] dp = new int[H][W];
    for(int i = 0; i < H; i++) {
      String s = sc.next();
      for(int j = 0; j < W; j++) {
        char c = s.charAt(j);
        board[i][j] = c;
        if(c == '.') {
          dp[i][j] = 5000; 
        }
      }
    }
    LinkedList<V> q = new LinkedList<V>();
    int[] xmove = {-1, 0, 1};
    int[] ymove = {-1, 0, 1};
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(dp[i][j] == 0) {
          if(i == 0 || i == H - 1 || j == 0 || j == W - 1) {
            dp[i][j] = 1;
            V v = new V(i, j);
            q.add(v);
          } else {
            boolean f = false;
            for(int k = 0; k < 3; k++) {
              for(int l = 0; l < 3; l++) {
                if(dp[i + xmove[k]][j + ymove[l]] == 5000) {
                  f = true;
                  dp[i][j] = 1;
                  V v = new V(i, j);
                  q.add(v);
                }
                if(f) break;
              }
              if(f) break;
            }
          }
        }
      }
    }
    while(q.size() > 0) {
      V v = q.poll();
      for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
          if(v.h + xmove[i] >= 0 && v.h + xmove[i] < H && v.w + ymove[j] >= 0 && v.w + ymove[j] < W) {
            if(dp[v.h + xmove[i]][v.w + ymove[j]] == 0) {
              dp[v.h + xmove[i]][v.w + ymove[j]] = dp[v.h][v.w] + 1;
              q.add(new V(v.h + xmove[i], v.w + ymove[j]));
            }
          }
        }
      }
    }
    int ans = 0;
    for(int i = 0; i < H; i++) {
      for(int j = 0; j < W; j++) {
        if(dp[i][j] != 5000) ans = Math.max(ans, dp[i][j]);
      }
    }
    System.out.println(ans);
  }
}

class V {
  int h;
  int w;

  V(int h, int w) {
    this.h = h;
    this.w = w;
  }
}
0