結果

問題 No.157 2つの空洞
ユーザー 37zigen37zigen
提出日時 2016-05-01 20:27:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,075 bytes
コンパイル時間 2,175 ms
コンパイル使用メモリ 77,420 KB
実行使用メモリ 54,360 KB
最終ジャッジ日時 2024-04-15 08:28:47
合計ジャッジ時間 5,984 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
54,248 KB
testcase_01 AC 139 ms
54,116 KB
testcase_02 AC 139 ms
54,136 KB
testcase_03 AC 137 ms
54,264 KB
testcase_04 AC 137 ms
54,040 KB
testcase_05 AC 138 ms
54,032 KB
testcase_06 AC 141 ms
53,792 KB
testcase_07 AC 139 ms
54,148 KB
testcase_08 AC 137 ms
54,232 KB
testcase_09 AC 139 ms
53,936 KB
testcase_10 AC 139 ms
54,144 KB
testcase_11 AC 140 ms
53,932 KB
testcase_12 AC 140 ms
54,072 KB
testcase_13 AC 143 ms
54,020 KB
testcase_14 AC 142 ms
54,360 KB
testcase_15 AC 142 ms
54,248 KB
testcase_16 AC 142 ms
54,220 KB
testcase_17 AC 154 ms
54,120 KB
testcase_18 AC 137 ms
54,132 KB
testcase_19 AC 142 ms
54,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Scanner;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	int[] dx={1,-1,0,0};
	int[] dy={0,0,1,-1};
	void dfs(char[][] map,int i,int j,char x){
		map[i][j]=x;
		for(int k=0;k<4;k++){
			if(map[i+dy[k]][j+dx[k]]=='.')dfs(map,i+dy[k],j+dx[k],x);
		}
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int w=sc.nextInt();
		int h=sc.nextInt();
		char[][] map=new char[h][w];
		for(int i=0;i<h;i++){
			map[i]=sc.next().toCharArray();
		}
		bi:for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='.'){
					dfs(map,i,j,'1');
					break bi;
				}
			}
		}
		bi:for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='.'){
					dfs(map,i,j,'2');
					break bi;
				}
			}
		}
		int ans=999999999;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]=='1'){
					for(int k=0;k<h;k++){
						for(int l=0;l<w;l++){
							if(map[k][l]=='2')ans=Math.min(ans, Math.abs(i-k)+Math.abs(j-l));
						}
					}
				}
			}
		}
		System.out.println(ans-1);
	}
}
0