結果

問題 No.157 2つの空洞
ユーザー jp_stejp_ste
提出日時 2015-03-17 00:43:26
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,563 bytes
コンパイル時間 4,741 ms
コンパイル使用メモリ 73,956 KB
実行使用メモリ 78,336 KB
最終ジャッジ日時 2023-09-11 08:30:40
合計ジャッジ時間 8,007 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
60,260 KB
testcase_01 AC 123 ms
55,744 KB
testcase_02 AC 124 ms
55,572 KB
testcase_03 AC 124 ms
55,544 KB
testcase_04 AC 122 ms
55,772 KB
testcase_05 AC 124 ms
56,072 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	static int W, H;
	static char C[][];
	static int mAns = Integer.MAX_VALUE;
	
	public static void main(String[] args) {
		readMap();
		
		int start[] = serchCavity();
		replaceCavity(start[0], start[1], '+');
		
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				if(C[i][j] == '.') {
					solve(j, i, 0);
				}
			}
		}
		System.out.println(mAns-1);
	}
	
	static void solve(int j, int i, int step) {
		
		if(i==0 || i==H-1) return;
		if(j==0 || j==W-1) return;
		
		if(C[i][j] == '+') {
			mAns = Math.min(mAns, step);
			return;
		}
		
		C[i][j] = '.';
		
		if(C[i][j+1] != '.') solve(j+1, i  , step+1);
		if(C[i+1][j] != '.') solve(j  , i+1, step+1);
		if(C[i][j-1] != '.') solve(j-1, i  , step+1);
		if(C[i-1][j] != '.') solve(j  , i-1, step+1);
		
		C[i][j] = '#';
	}
	
	static void readMap() {
		Scanner sc = new Scanner(System.in);
		W = sc.nextInt();
		H = sc.nextInt();
		C = new char[H][W];
		
		for(int i=0; i<H; i++) {
			String line = sc.next();
			for(int j=0; j<W; j++) {
				C[i][j] = line.charAt(j);
			}
		}
	}
		
	static int[] serchCavity() {
		int ret[] = new int[2];
		
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				if(C[i][j] == '.') {
					ret[0] = j;
					ret[1] = i;
					return ret;
				}
			}
		}
		throw new IllegalArgumentException();
	}
	
	static void replaceCavity(int j, int i, char v) {
		
		if(C[i][j] != '.') return;
		C[i][j] = v;
		
		replaceCavity(j+1, i  , v);
		replaceCavity(j  , i+1, v);
		replaceCavity(j-1, i  , v);
		replaceCavity(j  , i-1, v);
	}
}
0