結果

問題 No.157 2つの空洞
ユーザー 37zigen37zigen
提出日時 2016-08-25 16:59:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 3,249 ms
コンパイル使用メモリ 77,936 KB
実行使用メモリ 54,512 KB
最終ジャッジ日時 2024-04-25 15:20:55
合計ジャッジ時間 6,612 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
54,116 KB
testcase_01 AC 134 ms
54,412 KB
testcase_02 AC 130 ms
53,876 KB
testcase_03 AC 127 ms
54,512 KB
testcase_04 AC 111 ms
53,012 KB
testcase_05 AC 144 ms
54,368 KB
testcase_06 AC 131 ms
54,128 KB
testcase_07 AC 123 ms
54,424 KB
testcase_08 AC 124 ms
54,396 KB
testcase_09 AC 122 ms
54,236 KB
testcase_10 AC 112 ms
52,896 KB
testcase_11 AC 131 ms
54,036 KB
testcase_12 AC 119 ms
53,196 KB
testcase_13 AC 129 ms
54,396 KB
testcase_14 AC 129 ms
53,128 KB
testcase_15 AC 125 ms
54,000 KB
testcase_16 AC 125 ms
54,232 KB
testcase_17 AC 132 ms
54,056 KB
testcase_18 AC 128 ms
54,308 KB
testcase_19 AC 129 ms
54,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No100番台;

import java.util.Scanner;

public class Q157 {
	public static void main(String[] args) throws Exception {
		new Q157().solve();
	}

	int[] dx = { 1, -1, 0, 0 };
	int[] dy = { 0, 0, 1, -1 };

	void dfs(char[][] map, int i, int j, char x) {
		map[i][j] = x;
		for (int k = 0; k < 4; k++) {
			if (map[i + dy[k]][j + dx[k]] == '.')
				dfs(map, i + dy[k], j + dx[k], x);
		}
	}

	void solve() {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int h = sc.nextInt();
		char[][] map = new char[h][w];
		for (int i = 0; i < h; i++) {
			map[i] = sc.next().toCharArray();
		}
		bi: for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == '.') {
					dfs(map, i, j, '1');
					break bi;
				}
			}
		}
		bi: for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == '.') {
					dfs(map, i, j, '2');
					break bi;
				}
			}
		}
		int ans = 999999999;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == '1') {
					for (int k = 0; k < h; k++) {
						for (int l = 0; l < w; l++) {
							if (map[k][l] == '2')
								ans = Math.min(ans, Math.abs(i - k) + Math.abs(j - l));
						}
					}
				}
			}
		}
		System.out.println(ans - 1);
	}
}
0