結果

問題 No.402 最も海から遠い場所
ユーザー tentententen
提出日時 2020-10-27 20:35:13
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,790 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 76,460 KB
実行使用メモリ 751,264 KB
最終ジャッジ日時 2023-09-29 03:24:18
合計ジャッジ時間 14,538 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
62,980 KB
testcase_01 AC 128 ms
56,256 KB
testcase_02 AC 177 ms
55,464 KB
testcase_03 AC 127 ms
53,844 KB
testcase_04 AC 129 ms
55,832 KB
testcase_05 AC 132 ms
55,764 KB
testcase_06 AC 128 ms
55,804 KB
testcase_07 AC 129 ms
56,304 KB
testcase_08 AC 129 ms
55,924 KB
testcase_09 AC 130 ms
56,004 KB
testcase_10 AC 130 ms
55,888 KB
testcase_11 AC 171 ms
55,936 KB
testcase_12 AC 137 ms
56,132 KB
testcase_13 AC 285 ms
64,204 KB
testcase_14 AC 240 ms
58,848 KB
testcase_15 AC 745 ms
101,060 KB
testcase_16 AC 1,112 ms
111,192 KB
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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[][] field = new char[h][];
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[][] values = new int[h][w];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (field[i][j] == '.') {
                    queue.add(new Path(i, j, 0));
                }
            }
            Arrays.fill(values[i], Integer.MAX_VALUE);
            queue.add(new Path(i, 0, 1));
            queue.add(new Path(i, w - 1, 1));
        }
        for (int i = 0; i < w; i++) {
            queue.add(new Path(0, i, 1));
            queue.add(new Path(h - 1, i, 1));
        }
        int max = 0;
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (values[p.h][p.w] <= p.value) {
                continue;
            }
            values[p.h][p.w] = p.value;
            max = Math.max(max, p.value);
            if (p.h > 0 && values[p.h - 1][p.w] > p.value + 1) {
                queue.add(new Path(p.h - 1, p.w, p.value + 1));
            }
            if (p.h < h - 1 && values[p.h + 1][p.w] > p.value + 1) {
                queue.add(new Path(p.h + 1, p.w, p.value + 1));
            }
            if (p.w > 0 && values[p.h][p.w - 1] > p.value + 1) {
                queue.add(new Path(p.h, p.w - 1, p.value + 1));
            }
            if (p.w < w - 1 && values[p.h][p.w + 1] > p.value + 1) {
                queue.add(new Path(p.h, p.w + 1, p.value + 1));
            }
            if (p.h > 0 && p.w > 0 && values[p.h - 1][p.w - 1] > p.value + 1) {
                queue.add(new Path(p.h - 1, p.w - 1, p.value + 1));
            }
            if (p.h > 0 && p.w < w  - 1 && values[p.h - 1][p.w + 1] > p.value + 1) {
                queue.add(new Path(p.h - 1, p.w + 1, p.value + 1));
            }
            if (p.h < h - 1 && p.w > 0 && values[p.h + 1][p.w - 1] > p.value + 1) {
                queue.add(new Path(p.h + 1, p.w - 1, p.value + 1));
            }
            if (p.h < h - 1 && p.w < w - 1 && values[p.h + 1][p.w + 1] > p.value + 1) {
                queue.add(new Path(p.h + 1, p.w + 1, p.value + 1));
            }
        }
        System.out.println(max);
    }
    
    static class Path implements Comparable<Path> {
        int h;
        int w;
        int value;
        
        public Path(int h, int w, int value) {
            this.h = h;
            this.w = w;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
0