結果

問題 No.402 最も海から遠い場所
ユーザー tenten
提出日時 2020-10-27 20:41:08
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 2,864 bytes
コンパイル時間 3,976 ms
コンパイル使用メモリ 79,224 KB
実行使用メモリ 95,028 KB
最終ジャッジ日時 2024-07-21 22:02:38
合計ジャッジ時間 15,415 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

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