結果

問題 No.157 2つの空洞
ユーザー spaciaspacia
提出日時 2016-01-20 10:35:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,061 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 78,196 KB
実行使用メモリ 53,692 KB
最終ジャッジ日時 2023-10-21 13:28:41
合計ジャッジ時間 4,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
53,668 KB
testcase_01 AC 49 ms
53,676 KB
testcase_02 AC 50 ms
53,668 KB
testcase_03 AC 50 ms
53,668 KB
testcase_04 AC 50 ms
53,672 KB
testcase_05 AC 50 ms
53,668 KB
testcase_06 AC 53 ms
53,672 KB
testcase_07 AC 53 ms
53,692 KB
testcase_08 AC 54 ms
53,684 KB
testcase_09 AC 56 ms
53,668 KB
testcase_10 AC 52 ms
53,684 KB
testcase_11 AC 54 ms
53,684 KB
testcase_12 AC 55 ms
53,672 KB
testcase_13 AC 56 ms
53,688 KB
testcase_14 AC 56 ms
53,628 KB
testcase_15 AC 55 ms
53,680 KB
testcase_16 AC 56 ms
53,684 KB
testcase_17 AC 57 ms
53,692 KB
testcase_18 AC 55 ms
53,676 KB
testcase_19 AC 56 ms
53,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {
	
	static int h , w;
	static String[] field;
	static int[] dx = {0,0,-1,1};
	static int[] dy = {-1,1,0,0};
	static boolean[][] isSearched;
	
	public static void out (Object o) {
		System.out.println(o);
	}
	
	public static int[] getBlank () {
		int start = -1;
		ArrayList<Integer> list = new ArrayList<Integer>();
		main:for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (field[i].charAt(j) == '#' || isSearched[i][j]) continue;
				isSearched[i][j] = true;
				start = j << 5 | i;
				break main;
			}
		}
		list.add(start);
		Queue<Integer> queue = new LinkedList<Integer>();
		queue.add(start);
		while (!queue.isEmpty()) {
			int q = queue.poll();
			int x = q >> 5;
			int y = q & 31;
			for (int i = 0; i < dx.length; i++) {
				int moveX = x + dx[i];
				int moveY = y + dy[i];
				if (moveX < 0 || moveX >= w || moveY < 0 || moveY >= h) continue;
				if (field[moveY].charAt(moveX) == '#' || isSearched[moveY][moveX]) continue;
				list.add(moveX << 5 | moveY);
				isSearched[moveY][moveX] = true;
				queue.add(moveX << 5 | moveY);
			}
		}
		int[] ret = new int[list.size()];
		for (int i = 0; i < list.size(); i++)
			ret[i] = list.get(i);
		return ret;
	}
	
	public static int solve () {
		int ret = Integer.MAX_VALUE;
		int[] bl1 = getBlank();
		int[] bl2 = getBlank();
		for (int i = 0; i < bl1.length; i++) {
			int x1 = bl1[i] >> 5;
			int y1 = bl1[i] & 31;
			for (int j = 0; j < bl2.length; j++) {
			int x2 = bl2[j] >> 5;
			int y2 = bl2[j] & 31;
				ret = Math.min(ret , Math.abs(x1 - x2) + Math.abs(y1 - y2) - 1);
			}
		}
		return ret;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		String[] line = br.readLine().split(" ");
		w = Integer.parseInt(line[0]);
		h = Integer.parseInt(line[1]);
		field = new String[h];
		isSearched = new boolean[h][w];
		for (int i = 0; i < h; i++) {
			field[i] = br.readLine();
		}
		
		out(solve());
	}
	
}
0