結果

問題 No.157 2つの空洞
ユーザー nCk_cvnCk_cv
提出日時 2016-02-18 11:06:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,972 bytes
コンパイル時間 3,107 ms
コンパイル使用メモリ 79,052 KB
実行使用メモリ 57,696 KB
最終ジャッジ日時 2023-10-23 18:04:10
合計ジャッジ時間 6,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
57,436 KB
testcase_01 AC 127 ms
57,608 KB
testcase_02 AC 129 ms
57,308 KB
testcase_03 WA -
testcase_04 AC 126 ms
57,456 KB
testcase_05 AC 130 ms
57,200 KB
testcase_06 AC 128 ms
57,508 KB
testcase_07 AC 124 ms
57,340 KB
testcase_08 AC 125 ms
57,424 KB
testcase_09 AC 127 ms
55,192 KB
testcase_10 AC 124 ms
57,356 KB
testcase_11 AC 132 ms
57,536 KB
testcase_12 AC 133 ms
57,648 KB
testcase_13 AC 120 ms
56,248 KB
testcase_14 AC 131 ms
57,536 KB
testcase_15 WA -
testcase_16 AC 137 ms
57,508 KB
testcase_17 AC 138 ms
57,696 KB
testcase_18 WA -
testcase_19 AC 134 ms
57,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.awt.geom.*;
import java.io.*;
public class Main {
	static int[] vx = {1,0,-1,0};
	static int[] vy = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int h = sc.nextInt();
		char[][] map = new char[h][];
		for(int i = 0; i < h; i++) {
			map[i] = sc.next().toCharArray();
		}
		int[][] sMap = new int[h][w];
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				sMap[i][j] = 2 << 27;
			}
		}
		IN:for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] == '.') {
					dfsA(i,j,0,sMap,map);
					break IN;
				}
			}
		}
		
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] == '#' || sMap[i][j] != 0) continue;
				bfs(i,j,sMap,map);
			}
		}
		int MIN = 2 << 27;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] == '.' && sMap[i][j] != 0) MIN = Math.min(MIN, sMap[i][j]);
			}
		}
		System.out.println(MIN);
 	}
	static void bfs(int y, int x, int[][] sMap, char[][] map) {
		ArrayDeque<Data> queue = new ArrayDeque<Data>();
		queue.add(new Data(y,x,0));
		sMap[y][x] = 0;
		while(!queue.isEmpty()) {
			Data tmp = queue.pollFirst();
			for(int i = 0; i < 4; i++) {
				int tx = vx[i] + tmp.x;
				int ty = vy[i] + tmp.y;
				if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length) continue;
				if(sMap[ty][tx] <= tmp.count) continue;
				sMap[ty][tx] = tmp.count;
				queue.add(new Data(ty,tx,tmp.count+1));
			}
		}
	}
	static class Data {
		int y;
		int x;
		int count;
		Data(int a, int b,int c) {
			 y = a;
			 x = b;
			 count = c;
		}
	}
	static void dfsA(int y,int x, int f, int[][] sMap, char[][] map) {
		sMap[y][x] = f;
		for(int i = 0; i < 4; i++) {
			int tx = vx[i] + x;
			int ty = vy[i] + y;
			if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length || map[ty][tx] == '#' || sMap[ty][tx] != 0) continue;
			dfsA(ty,tx,f,sMap,map);
		}
	}
}
0