結果

問題 No.402 最も海から遠い場所
ユーザー jp_stejp_ste
提出日時 2016-08-12 04:08:55
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,540 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 79,596 KB
実行使用メモリ 718,916 KB
最終ジャッジ日時 2024-04-25 02:25:28
合計ジャッジ時間 18,549 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
41,048 KB
testcase_01 AC 99 ms
40,444 KB
testcase_02 AC 114 ms
41,512 KB
testcase_03 AC 106 ms
41,364 KB
testcase_04 AC 105 ms
41,228 KB
testcase_05 AC 107 ms
41,452 KB
testcase_06 AC 104 ms
41,312 KB
testcase_07 AC 104 ms
41,292 KB
testcase_08 AC 104 ms
40,204 KB
testcase_09 AC 98 ms
40,424 KB
testcase_10 AC 95 ms
40,316 KB
testcase_11 AC 111 ms
40,668 KB
testcase_12 AC 111 ms
41,268 KB
testcase_13 AC 184 ms
44,136 KB
testcase_14 AC 173 ms
42,512 KB
testcase_15 AC 329 ms
57,124 KB
testcase_16 AC 394 ms
65,108 KB
testcase_17 AC 1,660 ms
327,432 KB
testcase_18 AC 2,038 ms
242,524 KB
testcase_19 TLE -
testcase_20 AC 2,042 ms
136,200 KB
testcase_21 MLE -
権限があれば一括ダウンロードができます

ソースコード

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;
    
    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+2][W+2];
        ansMap = new int[H+2][W+2];
        LinkedList<Node> q = new LinkedList<>();
        
        for(int i=0; i<map.length; i++) {
            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 Node(j, i, 0));
                }
            }
        }
        
        for(int i=0; i<H; i++) {
            line = scan.nextLine();
            for(int j=0; j<W; j++) {
                map[i+1][j+1] = line.charAt(j);
                if(map[i+1][j+1] == '.') {
                    q.add(new Node(j+1, i+1, 0));
                } else {
                    ansMap[i+1][j+1] = -1;
                }
            }
        }
        
        int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
        int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
        int ans = 1;
        
        while(!q.isEmpty()) {
            
            Node now = q.poll();
           
            for(int i=0; i<dx.length; i++) {
                int nextX = now.x + dx[i];
                int nextY = now.y + dy[i];
                int nextV = now.v + 1;
                
                if(nextX >= 0 && nextX < ansMap[0].length && nextY >= 0 && nextY < ansMap.length && ansMap[nextY][nextX] == -1) {
                    ansMap[nextY][nextX] = nextV;
                    q.add(new Node(nextX, nextY, nextV));
                    ans = Math.max(ans, ansMap[nextY][nextX]);
                }
            }
        }
        
        //out();
        System.out.println(ans);
    }
    
    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();
        }
    }
    
    static class Node {
        int x, y, v;
        Node(int x, int y, int v) {
            this.x = x;
            this.y = y;
            this.v = v;
        }
    }
}
0