結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-10-14 13:12:28 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 131 ms / 2,000 ms |
| コード長 | 1,686 bytes |
| コンパイル時間 | 2,467 ms |
| コンパイル使用メモリ | 82,456 KB |
| 実行使用メモリ | 41,564 KB |
| 最終ジャッジ日時 | 2024-07-20 19:05:51 |
| 合計ジャッジ時間 | 5,419 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
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;
}
}
tenten