結果

問題 No.157 2つの空洞
ユーザー tsunabittsunabit
提出日時 2020-03-08 23:24:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 3,379 ms
コンパイル使用メモリ 76,040 KB
実行使用メモリ 56,316 KB
最終ジャッジ日時 2023-08-07 15:59:29
合計ジャッジ時間 6,977 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,792 KB
testcase_01 AC 119 ms
56,072 KB
testcase_02 AC 120 ms
56,228 KB
testcase_03 AC 119 ms
55,744 KB
testcase_04 AC 120 ms
56,276 KB
testcase_05 AC 121 ms
55,536 KB
testcase_06 AC 122 ms
55,740 KB
testcase_07 AC 121 ms
56,312 KB
testcase_08 AC 122 ms
56,120 KB
testcase_09 AC 121 ms
55,768 KB
testcase_10 AC 121 ms
55,596 KB
testcase_11 AC 122 ms
55,652 KB
testcase_12 AC 125 ms
55,644 KB
testcase_13 AC 124 ms
55,620 KB
testcase_14 AC 125 ms
55,592 KB
testcase_15 AC 125 ms
56,316 KB
testcase_16 AC 124 ms
55,976 KB
testcase_17 AC 134 ms
56,264 KB
testcase_18 AC 124 ms
55,800 KB
testcase_19 AC 125 ms
55,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No157 {
	public static int W;
	public static int H;
	public static int ans = 999;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		W = sc.nextInt();
		H = sc.nextInt();
		char[][] c = new char[H][W];
		
		String t = "";
		for(int i = 0; i < H; i++) {
			t += sc.next();
		}
		
		for(int i = 0; i < H; i++) {
			for(int j = 0; j < W; j++) {
				c[i][j] = t.charAt(W*i+j);
			}
		}
		
		loop: for(int i = 0; i < H; i++) {
			for(int j = 0; j < W; j++) {
				if(c[i][j] == '.') {
					dfs1(c, i, j);
					break loop;
				}
			}
		}
		for(int i = 0; i < H; i++) {
			for(int j = 0; j < W; j++) {
				if(c[i][j] == '1') {
					for(int k = 0; k < H; k++) {
						for(int l = 0; l < W; l++) {
							if(c[k][l] == '.') {
								ans = Math.min(ans, Math.abs(i-k)+Math.abs(j-l)-1);
							}
						}
					}
				}
			}
		}
		System.out.println(ans);
	}
	
	public static void dfs1(char[][] c, int h, int w) {
		if(c[h][w] == '.') {
			c[h][w] = '1';
		}
		if(h > 0 && c[h-1][w] == '.') dfs1(c, h-1, w);
		if(h < H-1 && c[h+1][w] == '.') dfs1(c, h+1, w);
		if(w < W-1 && c[h][w+1] == '.') dfs1(c, h, w+1);
		if(w > 0 && c[h][w-1] == '.') dfs1(c, h, w-1);
	}
}
0