結果

問題 No.157 2つの空洞
ユーザー tsunabittsunabit
提出日時 2020-03-08 23:24:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 3,409 ms
コンパイル使用メモリ 79,624 KB
実行使用メモリ 41,484 KB
最終ジャッジ日時 2024-04-25 09:41:47
合計ジャッジ時間 6,721 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
40,288 KB
testcase_01 AC 124 ms
41,272 KB
testcase_02 AC 122 ms
41,372 KB
testcase_03 AC 109 ms
40,008 KB
testcase_04 AC 119 ms
41,416 KB
testcase_05 AC 109 ms
40,304 KB
testcase_06 AC 121 ms
41,380 KB
testcase_07 AC 143 ms
40,864 KB
testcase_08 AC 114 ms
40,256 KB
testcase_09 AC 109 ms
40,128 KB
testcase_10 AC 118 ms
40,008 KB
testcase_11 AC 127 ms
41,128 KB
testcase_12 AC 113 ms
40,344 KB
testcase_13 AC 129 ms
41,184 KB
testcase_14 AC 128 ms
41,056 KB
testcase_15 AC 113 ms
40,608 KB
testcase_16 AC 125 ms
41,288 KB
testcase_17 AC 131 ms
41,484 KB
testcase_18 AC 128 ms
41,368 KB
testcase_19 AC 114 ms
40,532 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