結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
a3636tako
|
| 提出日時 | 2016-08-25 16:32:17 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 60 ms / 2,000 ms |
| コード長 | 3,239 bytes |
| コンパイル時間 | 2,390 ms |
| コンパイル使用メモリ | 78,696 KB |
| 実行使用メモリ | 37,224 KB |
| 最終ジャッジ日時 | 2024-11-08 02:22:24 |
| 合計ジャッジ時間 | 4,096 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
import java.io.*;
import java.util.*;
public class Main{
int W;
int H;
int[][] field;
int[] DX = {1, 0, -1, 0};
int[] DY = {0, 1, 0, -1};
public void solve(){
W = nextInt();
H = nextInt();
field = new int[H][W];
for(int i = 0; i < H; i++){
String str = next();
for(int j = 0; j < W; j++){
if(str.charAt(j) == '#'){
field[i][j] = -1;
}else{
field[i][j] = 0;
}
}
}
int cnt = 1;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(field[i][j] == 0){
paint(j, i, cnt);
cnt++;
}
}
}
int ans = Integer.MAX_VALUE;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(field[i][j] == 1){
Queue<int[]> queue = new ArrayDeque<>();
queue.add(new int[]{j, i, 0});
boolean[][] flg = new boolean[H][W];
flg[i][j] = true;
while(!queue.isEmpty()){
int[] e = queue.poll();
for(int k = 0; k < 4; k++){
int nx = e[0] + DX[k];
int ny = e[1] + DY[k];
if(0 <= nx && nx < W && 0 <= ny && ny < H && !flg[ny][nx]){
flg[ny][nx] = true;
if(field[ny][nx] == 2){
ans = Math.min(ans, e[2]);
}else if(field[ny][nx] == -1 && e[2] + 1 < ans){
queue.add(new int[]{nx, ny, e[2] + 1});
}
}
}
}
}
}
}
out.println(ans);
}
public void paint(int x, int y, int c){
if(0 <= x && x < W && 0 <= y && y <= H && field[y][x] == 0){
field[y][x] = c;
for(int i = 0; i < 4; i++){
paint(x + DX[i], y + DY[i], c);
}
}
}
private static PrintWriter out;
public static void main(String[] args){
out = new PrintWriter(System.out);
new Main().solve();
out.flush();
}
public static int nextInt(){
int num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(;i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10 + (c - '0');
}
return minus ? -num : num;
}
public static long nextLong(){
long num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(;i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10l + (c - '0');
}
return minus ? -num : num;
}
public static String next(){
int c;
while(!isAlNum(c = read())){}
StringBuilder build = new StringBuilder();
build.append((char)c);
while(isAlNum(c = read())){
build.append((char)c);
}
return build.toString();
}
private static byte[] inputBuffer = new byte[1024];
private static int bufferLength = 0;
private static int bufferIndex = 0;
private static int read(){
if(bufferLength < 0) throw new RuntimeException();
if(bufferIndex >= bufferLength){
try{
bufferLength = System.in.read(inputBuffer);
bufferIndex = 0;
}catch(IOException e){
throw new RuntimeException(e);
}
if(bufferLength <= 0) return (bufferLength = -1);
}
return inputBuffer[bufferIndex++];
}
private static boolean isAlNum(int c){
return '!' <= c && c <= '~';
}
}
a3636tako