結果

問題 No.402 最も海から遠い場所
ユーザー tenten
提出日時 2020-10-27 20:35:13
言語 Java
(openjdk 23)
結果
MLE  
実行時間 -
コード長 2,790 bytes
コンパイル時間 2,347 ms
コンパイル使用メモリ 79,348 KB
実行使用メモリ 638,376 KB
最終ジャッジ日時 2024-07-21 22:01:54
合計ジャッジ時間 12,709 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 MLE * 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();
        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