結果

問題 No.157 2つの空洞
ユーザー nCk_cvnCk_cv
提出日時 2016-02-18 11:07:42
言語 Java19
(openjdk 21)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 78,336 KB
実行使用メモリ 57,652 KB
最終ジャッジ日時 2023-10-23 18:04:17
合計ジャッジ時間 6,056 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
57,516 KB
testcase_01 AC 135 ms
57,456 KB
testcase_02 AC 135 ms
57,560 KB
testcase_03 AC 135 ms
57,428 KB
testcase_04 AC 135 ms
55,616 KB
testcase_05 AC 137 ms
57,504 KB
testcase_06 AC 139 ms
57,376 KB
testcase_07 AC 135 ms
57,612 KB
testcase_08 AC 135 ms
57,624 KB
testcase_09 AC 137 ms
57,520 KB
testcase_10 AC 133 ms
57,600 KB
testcase_11 AC 139 ms
57,504 KB
testcase_12 AC 141 ms
57,404 KB
testcase_13 AC 138 ms
57,612 KB
testcase_14 AC 144 ms
57,616 KB
testcase_15 AC 141 ms
57,636 KB
testcase_16 AC 142 ms
57,588 KB
testcase_17 AC 142 ms
57,544 KB
testcase_18 AC 141 ms
57,580 KB
testcase_19 AC 140 ms
57,652 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