結果

問題 No.402 最も海から遠い場所
ユーザー jp_stejp_ste
提出日時 2016-08-11 18:48:40
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,067 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 78,592 KB
実行使用メモリ 717,028 KB
最終ジャッジ日時 2024-04-25 01:33:18
合計ジャッジ時間 14,493 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 TLE -
testcase_20 RE -
testcase_21 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    static Scanner scan = new Scanner(System.in);
    static PrintWriter out = new PrintWriter(System.out);

    static int H, W;
    static char[][] map;
    static int [][] ansMap;
    static int ans = 0;
    
    public static void main(String[] args) {
        H = scan.nextInt();
        W = scan.nextInt();
        LinkedList<Point> q = new LinkedList<>();
        
        map = new char[H+2][W+2];
        ansMap = new int[H+2][W+2];
        
        for(int i=0; i<map.length; i++) {
            String line = scan.nextLine();
            for(int j=0; j<map[0].length; j++) {
                if(i==0 || i==map.length-1 || j==0 || j==map[0].length-1) { 
                    map[i][j] = '.';
                    q.add(new Point(j,i));
                    continue;
                }
                map[i][j] = line.charAt(j-1);
                
                if(map[i][j] == '.') {
                    q.add(new Point(j,i));
                }
            }
        }
        
        int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
        int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
        
        while(!q.isEmpty()) {
            
            Point now = q.poll();
            
            for(int i=0; i<dx.length; i++) {
                int nextX = now.x + dx[i];
                int nextY = now.y + dy[i];
                
                if(nextX >= 0 && nextX < map[0].length && nextY >= 0 && nextY < map.length) {
                    if(map[nextY][nextX] == '#' && ansMap[nextY][nextX] == 0) {
                        ansMap[nextY][nextX] = ansMap[now.y][now.x] + 1;
                        q.add(new Point(nextX, nextY));
                        ans = Math.max(ans, ansMap[nextY][nextX]);
                    }
                }
            }
            
        }
        System.out.println(ans);
    }
        
    static class Point {
        int x, y;
        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
0