結果

問題 No.157 2つの空洞
ユーザー リチウムリチウム
提出日時 2015-02-27 00:07:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,101 bytes
コンパイル時間 4,893 ms
コンパイル使用メモリ 73,940 KB
実行使用メモリ 55,968 KB
最終ジャッジ日時 2023-09-06 02:58:44
合計ジャッジ時間 8,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,612 KB
testcase_01 AC 120 ms
55,460 KB
testcase_02 AC 120 ms
55,712 KB
testcase_03 AC 121 ms
55,968 KB
testcase_04 AC 121 ms
55,724 KB
testcase_05 AC 122 ms
55,628 KB
testcase_06 AC 125 ms
55,560 KB
testcase_07 AC 123 ms
55,768 KB
testcase_08 AC 124 ms
55,720 KB
testcase_09 AC 125 ms
55,624 KB
testcase_10 AC 124 ms
55,812 KB
testcase_11 AC 125 ms
55,800 KB
testcase_12 AC 128 ms
55,636 KB
testcase_13 AC 126 ms
55,952 KB
testcase_14 AC 131 ms
55,540 KB
testcase_15 AC 126 ms
55,572 KB
testcase_16 AC 127 ms
55,792 KB
testcase_17 AC 135 ms
55,812 KB
testcase_18 AC 125 ms
55,784 KB
testcase_19 AC 126 ms
55,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class yukicoder {
	public static void dfs(char[][]map,int i,int j,char x){
		map[i][j]=x;
		int dx[]={0,0,1,-1};
		int dy[]={1,-1,0,0};
		for(int k=0;k<4;k++){
			if(map[i+dy[k]][j+dx[k]]=='.')dfs(map,i+dy[k],j+dx[k],x);
		}
	}
	
	
	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][w];
			for(int i=0;i<h;i++){
				char a[]=sc.next().toCharArray();
				for(int j=0;j<w;j++){
					map[i][j]=a[j];
				}
			}
			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;
					}
				}
			}
			
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					if(map[i][j]=='.')dfs(map,i,j,'2');
				}
			}
			int ans=10000000;
			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 p=0;p<w;p++){
								if(map[k][p]=='2')ans=Math.min(ans,Math.abs(i-k)+Math.abs(j-p));
							}
						}
					}
				}
			}
			
			System.out.println(ans-1);
			
			
			
	}

}
0