結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
nCk_cv
|
| 提出日時 | 2016-02-18 04:12:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,154 bytes |
| コンパイル時間 | 2,556 ms |
| コンパイル使用メモリ | 80,280 KB |
| 実行使用メモリ | 772,928 KB |
| 最終ジャッジ日時 | 2024-09-22 07:46:37 |
| 合計ジャッジ時間 | 10,451 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 8 MLE * 1 -- * 7 |
ソースコード
import java.util.*;
import java.awt.geom.*;
import java.io.*;
public class Main {
static int[] vx = {1,0,-1,0};
static int[] vy = {0,1,0,-1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int h = sc.nextInt();
char[][] map = new char[h][];
for(int i = 0; i < h; i++) {
map[i] = sc.next().toCharArray();
}
int[][] sMap = new int[h][w];
int count = 0;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
if(sMap[i][j] == 0 && map[i][j] == '.') dfsA(i,j,++count,sMap,map);
}
}
int MIN = 2 << 27;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
if(map[i][j] == '#' || sMap[i][j] != 1) continue;
MIN = Math.min(MIN, bfs(i,j,1,sMap,map));
}
}
System.out.println(MIN);
}
static int bfs(int y, int x, int f, int[][] sMap, char[][] map) {
ArrayDeque<Data> queue = new ArrayDeque<Data>();
int[][] cpM = new int[sMap.length][];
for(int i = 0; i < cpM.length; i++) {
cpM[i] = Arrays.copyOf(sMap[i],sMap[i].length);
}
queue.add(new Data(y,x,0,cpM));
cpM[y][x] = 1;
while(!queue.isEmpty()) {
Data tmp = queue.pollFirst();
for(int i = 0; i < 4; i++) {
int tx = vx[i] + tmp.x;
int ty = vy[i] + tmp.y;
if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length) continue;
if(tmp.sMap[ty][tx] == 2) return tmp.count;
if(tmp.sMap[ty][tx] == 1) continue;
int[][] cpMx = new int[sMap.length][];
for(int j = 0; j < cpM.length; j++) {
cpMx[j] = Arrays.copyOf(tmp.sMap[j],tmp.sMap[j].length);
}
cpMx[ty][tx] = 1;
queue.add(new Data(ty,tx,tmp.count+1,cpMx));
}
}
return 2 << 27;
}
static class Data {
int y;
int x;
int count;
int[][] sMap;
Data(int a, int b,int c, int[][] d) {
y = a;
x = b;
count = c;
sMap = d;
}
}
static void dfsA(int y,int x, int f, int[][] sMap, char[][] map) {
sMap[y][x] = f;
for(int i = 0; i < 4; i++) {
int tx = vx[i] + x;
int ty = vy[i] + y;
if(tx < 0 || ty < 0 || ty >= map.length || tx >= map[ty].length || map[ty][tx] == '#' || sMap[ty][tx] != 0) continue;
dfsA(ty,tx,f,sMap,map);
}
}
}
nCk_cv