結果

問題 No.157 2つの空洞
ユーザー dumshdumsh
提出日時 2016-11-19 21:37:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,439 bytes
コンパイル時間 2,387 ms
コンパイル使用メモリ 77,412 KB
実行使用メモリ 37,288 KB
最終ジャッジ日時 2024-05-05 10:13:40
合計ジャッジ時間 4,383 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,044 KB
testcase_01 AC 53 ms
36,704 KB
testcase_02 AC 53 ms
37,024 KB
testcase_03 AC 53 ms
36,808 KB
testcase_04 AC 54 ms
37,108 KB
testcase_05 AC 53 ms
37,076 KB
testcase_06 AC 53 ms
36,760 KB
testcase_07 AC 52 ms
36,788 KB
testcase_08 AC 55 ms
36,828 KB
testcase_09 AC 54 ms
37,040 KB
testcase_10 AC 54 ms
36,728 KB
testcase_11 AC 55 ms
36,764 KB
testcase_12 AC 55 ms
36,928 KB
testcase_13 AC 55 ms
37,128 KB
testcase_14 AC 56 ms
37,076 KB
testcase_15 AC 57 ms
37,288 KB
testcase_16 AC 56 ms
36,840 KB
testcase_17 AC 59 ms
37,204 KB
testcase_18 AC 56 ms
37,192 KB
testcase_19 AC 56 ms
36,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String wh[] = br.readLine().split(" ");
		int w = Integer.parseInt(wh[0]);
		int h = Integer.parseInt(wh[1]);
		int space[][] = new int[w][h];
		boolean first = true;
		for (int i = 0; i < h; i++) {
			String line[] = br.readLine().split("");
			for (int k = 0; k < w; k++) {
				if (line[k].equals("#")) {
					space[k][i] = 0;
				} else {
					if (first) {
						space[k][i] = 1;
						first = false;
					} else {
						space[k][i] = 2;
					}
				}
			}
		}
		int xy[] = {1, 0, -1, 0, 1};
		while (true) {
			boolean all = true;
			for (int i = 1; i < h-1; i++) {
				for (int k = 1; k < w-1; k++) {
					if (space[k][i] == 2) {
						for (int l = 0; l < xy.length - 1; l++) {
							if (space[k+xy[l]][i+xy[l+1]] == 1) {
								space[k][i] = 1;
								all = false;
							}
						}
					}
				}
			}
			if (all) {
				break;
			}
		}
		int dis = w + h;
		for (int i = 1; i < h-1; i++) {
			for (int k = 1; k < w-1; k++) {
				if (space[k][i] == 1) {
					for (int p = 1; p < h-1; p++) {
						for (int q = 1; q < w-1; q++) {
							if (space[q][p] == 2) {
								int tance = Math.abs(i-p)+Math.abs(k-q)-1;
								if (tance < dis) {
									dis = tance;
								}
							}
						}
					}
				}
			}
		}
		System.out.println(dis);
	}
}
0