結果

問題 No.402 最も海から遠い場所
ユーザー jp_stejp_ste
提出日時 2016-08-12 15:54:40
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,656 ms / 3,000 ms
コード長 3,062 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 74,204 KB
実行使用メモリ 135,704 KB
最終ジャッジ日時 2023-08-07 08:29:42
合計ジャッジ時間 12,880 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,696 KB
testcase_01 AC 114 ms
56,080 KB
testcase_02 AC 123 ms
55,904 KB
testcase_03 AC 114 ms
55,912 KB
testcase_04 AC 114 ms
55,812 KB
testcase_05 AC 116 ms
56,008 KB
testcase_06 AC 115 ms
55,740 KB
testcase_07 AC 116 ms
56,212 KB
testcase_08 AC 115 ms
55,980 KB
testcase_09 AC 115 ms
55,856 KB
testcase_10 AC 115 ms
55,692 KB
testcase_11 AC 120 ms
56,224 KB
testcase_12 AC 120 ms
55,680 KB
testcase_13 AC 202 ms
56,304 KB
testcase_14 AC 168 ms
55,836 KB
testcase_15 AC 310 ms
59,680 KB
testcase_16 AC 390 ms
62,480 KB
testcase_17 AC 949 ms
98,144 KB
testcase_18 AC 2,656 ms
135,060 KB
testcase_19 AC 1,156 ms
133,104 KB
testcase_20 AC 777 ms
135,064 KB
testcase_21 AC 1,136 ms
135,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
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[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
    static int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
    
    public static void main(String[] args) {
        
        String line = scan.nextLine();
        String values[] = line.split(" ");
        H = Integer.parseInt(values[0]);
        W = Integer.parseInt(values[1]);;
        
        map = new char[H][W];
        ansMap = new int[H][W];
        
        for(int i=0; i<H; i++) {
            line = scan.nextLine();
            for(int j=0; j<W; j++) {
                map[i][j] = line.charAt(j);
                if(map[i][j] == '#') {
                    ansMap[i][j] = -1;
                }
                if((i==0 || i==H-1 || j==0 || j==W-1) && map[i][j] == '#') { 
                    ansMap[i][j] = 1;
                }
            }
        }
        
        boolean dotFind = false;
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                if(map[i][j] == '.') {
                    dotFind = true;
                    for(int k=0; k<dx.length; k++) {
                        int nextX = j + dx[k];
                        int nextY = i + dy[k];
                        
                        if(nextX >= 0 && nextX < W && nextY >= 0 && nextY < H && map[nextY][nextX] == '#') {
                            ansMap[nextY][nextX] = 1;
                        }
                    }
                }
            }
        }
        if(!dotFind) {
            int minLen = Math.min(W, H);
            System.out.println(minLen==1? 1 : minLen/2);
            return;
        }
        
        
        for(int count=1; count<=1500; count++) {
            //out();
            
            boolean find = false;
            
            for(int i=0; i<H; i++) {
                for(int j=0; j<W; j++) {
                    
                    if(ansMap[i][j] == count) {
                        for(int k=0; k<dx.length; k++) {
                            int nextX = j + dx[k];
                            int nextY = i + dy[k];
                            
                            if(nextX >= 0 && nextX < W && nextY >= 0 && nextY < H && ansMap[nextY][nextX] == -1) {
                                ansMap[nextY][nextX] = (count+1);
                                find = true;
                            }
                        }    
                    }
                }
            }
            if(find == false) {
                System.out.println(count);
                break;
            }
        }
    }
    static void out() {
        for(int i=0; i<ansMap.length; i++) {
            for(int j=0; j<ansMap[0].length; j++) {
                System.out.printf("%2d " , ansMap[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }
}
0