結果

問題 No.402 最も海から遠い場所
ユーザー tentententen
提出日時 2020-10-27 20:41:08
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,864 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 75,760 KB
実行使用メモリ 109,772 KB
最終ジャッジ日時 2023-09-29 03:24:53
合計ジャッジ時間 13,977 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,716 KB
testcase_01 AC 128 ms
55,468 KB
testcase_02 AC 173 ms
55,684 KB
testcase_03 AC 129 ms
55,948 KB
testcase_04 AC 134 ms
55,604 KB
testcase_05 AC 133 ms
55,856 KB
testcase_06 AC 130 ms
55,684 KB
testcase_07 AC 129 ms
55,664 KB
testcase_08 AC 126 ms
55,836 KB
testcase_09 AC 132 ms
55,496 KB
testcase_10 AC 129 ms
55,684 KB
testcase_11 AC 158 ms
55,716 KB
testcase_12 AC 136 ms
55,768 KB
testcase_13 AC 295 ms
63,092 KB
testcase_14 AC 248 ms
58,728 KB
testcase_15 AC 738 ms
99,200 KB
testcase_16 AC 1,134 ms
109,772 KB
testcase_17 TLE -
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();
        PriorityQueue<Path> queue = new PriorityQueue<>();
        short[][] values = new short[h][w];
        for (int i = 0; i < h; i++) {
            char[] field = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (field[j] == '.') {
                    queue.add(new Path(i, j, (short)0));
                }
            }
            Arrays.fill(values[i], Short.MAX_VALUE);
            queue.add(new Path(i, 0, (short)1));
            queue.add(new Path(i, w - 1, (short)1));
        }
        for (int i = 0; i < w; i++) {
            queue.add(new Path(0, i, (short)1));
            queue.add(new Path(h - 1, i, (short)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, (short)(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, (short)(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, (short)(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, (short)(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, (short)(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, (short)(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, (short)(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, (short)(p.value + 1)));
            }
        }
        System.out.println(max);
    }
    
    static class Path implements Comparable<Path> {
        int h;
        int w;
        short value;
        
        public Path(int h, int w, short value) {
            this.h = h;
            this.w = w;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
0