結果

問題 No.157 2つの空洞
ユーザー dumshdumsh
提出日時 2016-11-19 21:37:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,439 bytes
コンパイル時間 4,908 ms
コンパイル使用メモリ 73,728 KB
実行使用メモリ 49,896 KB
最終ジャッジ日時 2023-08-18 03:44:57
合計ジャッジ時間 4,388 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,352 KB
testcase_01 AC 42 ms
49,896 KB
testcase_02 AC 41 ms
49,748 KB
testcase_03 AC 41 ms
49,348 KB
testcase_04 AC 44 ms
49,272 KB
testcase_05 AC 42 ms
49,296 KB
testcase_06 AC 43 ms
49,440 KB
testcase_07 AC 43 ms
49,344 KB
testcase_08 AC 43 ms
49,556 KB
testcase_09 AC 43 ms
49,436 KB
testcase_10 AC 43 ms
49,676 KB
testcase_11 AC 42 ms
49,456 KB
testcase_12 AC 43 ms
49,536 KB
testcase_13 AC 43 ms
47,548 KB
testcase_14 AC 44 ms
49,380 KB
testcase_15 AC 44 ms
49,340 KB
testcase_16 AC 43 ms
49,560 KB
testcase_17 AC 46 ms
49,280 KB
testcase_18 AC 43 ms
49,336 KB
testcase_19 AC 43 ms
49,224 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