結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
neko_the_shadow
|
| 提出日時 | 2019-05-20 17:12:11 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 158 ms / 2,000 ms |
| コード長 | 2,700 bytes |
| コンパイル時間 | 2,310 ms |
| コンパイル使用メモリ | 79,524 KB |
| 実行使用メモリ | 41,456 KB |
| 最終ジャッジ日時 | 2024-09-17 06:40:09 |
| 合計ジャッジ時間 | 5,865 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int w = stdin.nextInt();
int h = stdin.nextInt();
char[][] matrix = new char[h][w];
for (int i = 0; i < h; i++) {
matrix[i] = stdin.next().toCharArray();
}
Set<Tuple> blanks1 = new HashSet<>();
Set<Tuple> blanks2 = new HashSet<>();
ArrayDeque<Tuple> stack = new ArrayDeque<>();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (matrix[i][j] != '.') {
continue;
}
if (blanks1.isEmpty()) {
blanks1.add(new Tuple(i, j));
stack.addFirst(new Tuple(i, j));
} else {
blanks2.add(new Tuple(i, j));
}
}
}
List<Tuple> diffs = new ArrayList<>();
diffs.add(new Tuple( 1, 0));
diffs.add(new Tuple(-1, 0));
diffs.add(new Tuple( 0, 1));
diffs.add(new Tuple( 0, -1));
while (!stack.isEmpty()) {
Tuple t = stack.removeFirst();
for (Tuple d : diffs) {
int x = t.x + d.x;
int y = t.y + d.y;
Tuple n = new Tuple(x, y);
if (0 <= x && x < h && 0 <= y && y < w && blanks2.contains(n)) {
stack.add(n);
blanks1.add(n);
blanks2.remove(n);
}
}
}
int ans = Integer.MAX_VALUE;
for (Tuple blank1 : blanks1) {
for (Tuple blank2 : blanks2) {
int len = Math.abs(blank1.x - blank2.x) + Math.abs(blank1.y - blank2.y);
ans = Math.min(ans, len);
}
}
System.out.println(ans - 1);
}
private static class Tuple {
private int x;
private int y;
public Tuple(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public boolean equals(Object obj) {
if ((obj == null) || !(obj instanceof Tuple)) {
return false;
}
Tuple other = (Tuple) obj;
return this.x == other.x && this.y == other.y;
}
}
}
neko_the_shadow