結果

問題 No.402 最も海から遠い場所
ユーザー tentententen
提出日時 2021-05-17 18:12:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 619 ms / 3,000 ms
コード長 2,587 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 78,188 KB
実行使用メモリ 100,984 KB
最終ジャッジ日時 2024-04-16 06:59:00
合計ジャッジ時間 7,847 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,996 KB
testcase_01 AC 52 ms
36,868 KB
testcase_02 AC 56 ms
36,616 KB
testcase_03 AC 53 ms
36,752 KB
testcase_04 AC 52 ms
36,620 KB
testcase_05 AC 52 ms
37,144 KB
testcase_06 AC 51 ms
36,628 KB
testcase_07 AC 53 ms
36,972 KB
testcase_08 AC 53 ms
37,204 KB
testcase_09 AC 51 ms
36,888 KB
testcase_10 AC 52 ms
37,216 KB
testcase_11 AC 56 ms
37,348 KB
testcase_12 AC 52 ms
37,216 KB
testcase_13 AC 82 ms
38,420 KB
testcase_14 AC 72 ms
38,164 KB
testcase_15 AC 201 ms
41,660 KB
testcase_16 AC 221 ms
43,024 KB
testcase_17 AC 496 ms
71,336 KB
testcase_18 AC 576 ms
100,396 KB
testcase_19 AC 619 ms
100,904 KB
testcase_20 AC 592 ms
100,888 KB
testcase_21 AC 591 ms
100,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
        }
        int[][] dists = new int[h + 2][w + 2];
        for (int i = 0; i < h + 2; i++) {
            if (i >= 1 && i <= h) {
                Arrays.fill(dists[i], Integer.MAX_VALUE);
                dists[i][0] = 0;
                dists[i][w + 1] = 0;
                for (int j = 0; j < w; j++) {
                    if (field[i - 1][j] == '.') {
                        dists[i][j + 1] = 0;
                    }
                }
            }
        }
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                int min = Math.min(dists[i][j - 1], Math.min(dists[i - 1][j - 1], dists[i - 1][j]));
                dists[i][j] = Math.min(dists[i][j], min + 1);
            }
            for (int j = w; j>= 1; j--) {
                int min = Math.min(dists[i][j + 1], Math.min(dists[i - 1][j + 1], dists[i - 1][j]));
                dists[i][j] = Math.min(dists[i][j], min + 1);
            }
        }
        for (int i = h; i >= 1; i--) {
            for (int j = 1; j <= w; j++) {
                int min = Math.min(dists[i][j - 1], Math.min(dists[i + 1][j - 1], dists[i + 1][j]));
                dists[i][j] = Math.min(dists[i][j], min + 1);
            }
            for (int j = w; j>= 1; j--) {
                int min = Math.min(dists[i][j + 1], Math.min(dists[i + 1][j + 1], dists[i + 1][j]));
                dists[i][j] = Math.min(dists[i][j], min + 1);
            }
        }
        int ans = 0;
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                ans = Math.max(ans, dists[i][j]);
            }
        }
        System.out.println(ans);
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0