結果

問題 No.157 2つの空洞
ユーザー htensaihtensai
提出日時 2020-02-14 08:34:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 79,376 KB
実行使用メモリ 42,000 KB
最終ジャッジ日時 2024-04-16 00:06:41
合計ジャッジ時間 6,114 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,600 KB
testcase_01 AC 134 ms
41,668 KB
testcase_02 AC 133 ms
41,656 KB
testcase_03 AC 134 ms
41,612 KB
testcase_04 AC 132 ms
42,000 KB
testcase_05 AC 140 ms
41,484 KB
testcase_06 AC 140 ms
41,516 KB
testcase_07 AC 138 ms
41,508 KB
testcase_08 AC 140 ms
41,324 KB
testcase_09 AC 138 ms
41,472 KB
testcase_10 AC 135 ms
41,736 KB
testcase_11 AC 137 ms
41,392 KB
testcase_12 AC 137 ms
41,636 KB
testcase_13 AC 146 ms
41,420 KB
testcase_14 AC 151 ms
41,760 KB
testcase_15 AC 151 ms
41,892 KB
testcase_16 AC 143 ms
41,524 KB
testcase_17 AC 146 ms
41,764 KB
testcase_18 AC 145 ms
41,504 KB
testcase_19 AC 139 ms
41,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int h = sc.nextInt();
		char[][] field = new char[h][];
		for (int i = 0; i < h; i++) {
		    field[i] = sc.next().toCharArray();
		}
		ArrayDeque<Integer> deq = new ArrayDeque<>();
		for (int i = 1; i < h - 1 && deq.size() == 0; i++) {
		    for (int j = 1; j < w - 1 && deq.size() == 0; j++) {
		        if (field[i][j] == '.') {
		            deq.add(i * w + j);
		        }
		    }
		}
		HashSet<Integer> holeA = new HashSet<>();
		while (deq.size() > 0) {
		    int x = deq.poll();
		    if (field[x / w][x % w] == '.' && !holeA.contains(x)) {
		        holeA.add(x);
		        deq.add(x - 1);
		        deq.add(x + 1);
		        deq.add(x - w);
		        deq.add(x + w);
		    }
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i < h - 1; i++) {
		    for (int j = 1; j < w - 1; j++) {
		        if (field[i][j] == '.' && !holeA.contains(i * w + j)) {
		            for (int x : holeA) {
		                min = Math.min(min, Math.abs(x / w - i) + Math.abs(x % w - j));
		            }
		        }
		    }
		}
		System.out.println(min - 1);
   }
}
0