結果

問題 No.157 2つの空洞
ユーザー a3636takoa3636tako
提出日時 2016-08-25 16:32:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 3,239 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 75,156 KB
実行使用メモリ 49,604 KB
最終ジャッジ日時 2023-08-07 20:57:56
合計ジャッジ時間 3,713 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,580 KB
testcase_01 AC 40 ms
49,124 KB
testcase_02 AC 41 ms
49,512 KB
testcase_03 AC 40 ms
49,040 KB
testcase_04 AC 40 ms
49,052 KB
testcase_05 AC 40 ms
47,296 KB
testcase_06 AC 41 ms
49,188 KB
testcase_07 AC 42 ms
48,976 KB
testcase_08 AC 41 ms
49,064 KB
testcase_09 AC 40 ms
49,252 KB
testcase_10 AC 40 ms
49,068 KB
testcase_11 AC 41 ms
49,044 KB
testcase_12 AC 41 ms
49,220 KB
testcase_13 AC 42 ms
49,204 KB
testcase_14 AC 46 ms
49,188 KB
testcase_15 AC 43 ms
49,356 KB
testcase_16 AC 45 ms
49,292 KB
testcase_17 AC 45 ms
49,428 KB
testcase_18 AC 43 ms
49,356 KB
testcase_19 AC 41 ms
49,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 <= '~';
	}
}
0