結果

問題 No.157 2つの空洞
ユーザー nCk_cvnCk_cv
提出日時 2016-02-18 04:12:40
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,154 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 79,892 KB
実行使用メモリ 372,888 KB
最終ジャッジ日時 2023-10-22 06:30:29
合計ジャッジ時間 11,424 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
57,324 KB
testcase_01 AC 128 ms
57,636 KB
testcase_02 AC 130 ms
57,472 KB
testcase_03 AC 135 ms
57,684 KB
testcase_04 AC 130 ms
57,404 KB
testcase_05 AC 135 ms
57,696 KB
testcase_06 AC 1,056 ms
302,624 KB
testcase_07 AC 132 ms
57,704 KB
testcase_08 AC 129 ms
57,520 KB
testcase_09 AC 134 ms
57,724 KB
testcase_10 AC 129 ms
57,676 KB
testcase_11 AC 1,378 ms
372,888 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
		int count = 0;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(sMap[i][j] == 0 && map[i][j] == '.') dfsA(i,j,++count,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] != 1) continue;
				MIN = Math.min(MIN, bfs(i,j,1,sMap,map));
			}
		}
		System.out.println(MIN);
 	}
	static int bfs(int y, int x, int f, int[][] sMap, char[][] map) {
		ArrayDeque<Data> queue = new ArrayDeque<Data>();
		int[][] cpM = new int[sMap.length][];
		for(int i = 0; i < cpM.length; i++) {
			cpM[i] = Arrays.copyOf(sMap[i],sMap[i].length);
		}
		queue.add(new Data(y,x,0,cpM));
		cpM[y][x] = 1;
		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(tmp.sMap[ty][tx] == 2) return tmp.count;
				if(tmp.sMap[ty][tx] == 1) continue;
				int[][] cpMx = new int[sMap.length][];
				for(int j = 0; j < cpM.length; j++) {
					cpMx[j] = Arrays.copyOf(tmp.sMap[j],tmp.sMap[j].length);
				}
				cpMx[ty][tx] = 1;
				queue.add(new Data(ty,tx,tmp.count+1,cpMx));
			}
		}
		return 2 << 27;
	}
	static class Data {
		int y;
		int x;
		int count;
		int[][] sMap;
		Data(int a, int b,int c, int[][] d) {
			 y = a;
			 x = b;
			 count = c;
			 sMap = d;
		}
	}
	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