結果

問題 No.157 2つの空洞
ユーザー tentententen
提出日時 2020-10-14 13:12:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 76,284 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2023-09-28 00:35:01
合計ジャッジ時間 5,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
55,916 KB
testcase_01 AC 109 ms
56,192 KB
testcase_02 AC 112 ms
56,400 KB
testcase_03 AC 111 ms
56,060 KB
testcase_04 AC 105 ms
55,984 KB
testcase_05 AC 109 ms
55,992 KB
testcase_06 AC 108 ms
55,980 KB
testcase_07 AC 112 ms
56,324 KB
testcase_08 AC 108 ms
56,124 KB
testcase_09 AC 113 ms
55,776 KB
testcase_10 AC 109 ms
55,940 KB
testcase_11 AC 111 ms
55,972 KB
testcase_12 AC 111 ms
56,048 KB
testcase_13 AC 112 ms
55,836 KB
testcase_14 AC 115 ms
56,084 KB
testcase_15 AC 117 ms
56,048 KB
testcase_16 AC 113 ms
55,684 KB
testcase_17 AC 122 ms
56,176 KB
testcase_18 AC 118 ms
55,556 KB
testcase_19 AC 113 ms
55,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int w = sc.nextInt();
    	int h = sc.nextInt();
    	char[][] field = new char[h][];
    	for (int i = 0; i < h; i++) {
    	    field[i] = sc.next().toCharArray();
    	}
    	HashSet<Integer> lefts = new HashSet<>();
    	HashSet<Integer> rights = new HashSet<>();
    	boolean used = false;
    	for (int i = 0; i < h; i++) {
    	    for (int j = 0; j < w; j++) {
    	        if (field[i][j] == '.') {
    	            HashSet<Integer> tmp;
    	            if (used) {
    	                tmp = rights;
    	            } else {
    	                tmp = lefts;
    	                used = true;
    	            }
    	            PriorityQueue<Integer> queue = new PriorityQueue<>();
    	            queue.add(i * w + j);
    	            while (queue.size() > 0) {
    	                int x = queue.poll();
    	                if (field[x / w][x % w] == '#') {
    	                    continue;
    	                }
    	                field[x / w][x % w] = '#';
    	                queue.add(x + 1);
    	                queue.add(x - 1);
    	                queue.add(x + w);
    	                queue.add(x - w);
    	                tmp.add(x);
    	            }
    	        }
    	    }
    	}
    	int min = Integer.MAX_VALUE;
    	for (int x : lefts) {
    	    for (int y : rights) {
    	        min = Math.min(min, getDist(x, y, w));
    	    }
    	}
    	System.out.println(min);
	}
	
	static int getDist(int x, int y, int width) {
	    return Math.abs(x / width - y / width) + Math.abs(x % width - y % width) - 1;
	}
}
0