結果
問題 | No.157 2つの空洞 |
ユーザー | spacia |
提出日時 | 2016-01-20 10:05:29 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,190 bytes |
コンパイル時間 | 2,273 ms |
コンパイル使用メモリ | 78,292 KB |
実行使用メモリ | 37,340 KB |
最終ジャッジ日時 | 2024-09-21 14:43:29 |
合計ジャッジ時間 | 4,086 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,828 KB |
testcase_01 | AC | 53 ms
37,264 KB |
testcase_02 | AC | 54 ms
36,700 KB |
testcase_03 | AC | 54 ms
36,704 KB |
testcase_04 | AC | 52 ms
37,176 KB |
testcase_05 | AC | 52 ms
37,008 KB |
testcase_06 | AC | 52 ms
37,020 KB |
testcase_07 | AC | 55 ms
36,852 KB |
testcase_08 | AC | 52 ms
37,136 KB |
testcase_09 | AC | 53 ms
37,160 KB |
testcase_10 | AC | 53 ms
37,128 KB |
testcase_11 | AC | 52 ms
37,072 KB |
testcase_12 | AC | 53 ms
37,332 KB |
testcase_13 | AC | 53 ms
37,104 KB |
testcase_14 | AC | 52 ms
36,972 KB |
testcase_15 | AC | 53 ms
37,132 KB |
testcase_16 | AC | 52 ms
37,284 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
ソースコード
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 & 15; 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(); //out(bl1.length + "," + bl2.length); for (int i = 0; i < bl1.length; i++) { int x1 = bl1[i] >> 5; int y1 = bl1[i] & 15; //out(x1 + "," + y1); for (int j = 0; j < bl2.length; j++) { int x2 = bl2[j] >> 5; int y2 = bl2[j] & 15; //out("(" + x1 + "," + y1 + ") - (" + x2 + "," + y2 + ")"); 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()); } }