結果

問題 No.157 2つの空洞
ユーザー kou6839kou6839
提出日時 2015-03-05 00:10:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 2,520 ms
コンパイル使用メモリ 75,676 KB
実行使用メモリ 56,136 KB
最終ジャッジ日時 2023-09-06 13:24:34
合計ジャッジ時間 6,259 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,020 KB
testcase_01 AC 120 ms
55,840 KB
testcase_02 AC 120 ms
54,308 KB
testcase_03 AC 120 ms
55,988 KB
testcase_04 AC 121 ms
55,908 KB
testcase_05 AC 120 ms
55,688 KB
testcase_06 AC 121 ms
55,892 KB
testcase_07 AC 121 ms
56,136 KB
testcase_08 AC 120 ms
55,768 KB
testcase_09 AC 126 ms
55,752 KB
testcase_10 AC 124 ms
55,712 KB
testcase_11 AC 120 ms
55,620 KB
testcase_12 AC 122 ms
55,476 KB
testcase_13 AC 123 ms
55,712 KB
testcase_14 AC 124 ms
55,840 KB
testcase_15 AC 127 ms
54,376 KB
testcase_16 AC 124 ms
55,792 KB
testcase_17 AC 125 ms
55,592 KB
testcase_18 AC 124 ms
55,816 KB
testcase_19 AC 124 ms
55,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.beans.IntrospectionException;
import java.util.*;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int c = sc.nextInt();
		int r = sc.nextInt();
		char[][] map = new char[r][c];
		for(int i=0;i<r;i++){
			String string = sc.next();
			for(int j=0;j<c;j++){
				map[i][j]=string.charAt(j);
			}
		}
		int[] dx = {0,-1,0,1};
		int[] dy = {1,0,-1,0};
		ArrayList<Integer> r1 = new ArrayList<>();
		ArrayList<Integer> c1 = new ArrayList<>();
		ArrayList<Integer> r2 = new ArrayList<>();
		ArrayList<Integer> c2 = new ArrayList<>();
		boolean first = true;
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				if(map[i][j]=='.'&&first){
					Queue<Integer> rque = new LinkedList<>();
					Queue<Integer> cque = new LinkedList<>();
					rque.add(i);
					cque.add(j);
					r1.add(i);
					c1.add(j);
					while(!rque.isEmpty()){
						int rt = rque.poll();
						int ct = cque.poll();
						map[rt][ct]='x';
						for(int k=0;k<4;k++){
							if(rt+dx[k]>0 && rt+dx[k]<r&&ct+dy[k]>0&&ct+dy[k]<c&&map[rt+dx[k]][ct+dy[k]]=='.'){
								rque.offer(rt+dx[k]);
								cque.offer(ct+dy[k]);
								r1.add(rt+dx[k]);
								c1.add(ct+dy[k]);
								map[rt+dx[k]][ct+dy[k]]='x';
							}
						}
					}
					first=false;
				}else if(map[i][j]=='.'&&!first){
					Queue<Integer> rque = new LinkedList<>();
					Queue<Integer> cque = new LinkedList<>();
					rque.add(i);
					cque.add(j);
					r2.add(i);
					c2.add(j);
					while(!rque.isEmpty()){
						int rt = rque.poll();
						int ct = cque.poll();
						map[rt][ct]='x';
						for(int k=0;k<4;k++){
							if(rt+dx[k]>0 && rt+dx[k]<r&&ct+dy[k]>0&&ct+dy[k]<c&&map[rt+dx[k]][ct+dy[k]]=='.'){
								rque.offer(rt+dx[k]);
								cque.offer(ct+dy[k]);
								r2.add(rt+dx[k]);
								c2.add(ct+dy[k]);
							}
						}
					}
				}
			}
		}
		int ans=100;
		for(int i=0;i<r1.size();i++){
			for(int j=0;j<r2.size();j++){
				ans=Math.min(ans, Math.abs(r1.get(i)-r2.get(j))+Math.abs(c1.get(i)-c2.get(j)));
			}
		}
		System.out.println(ans-1);
	}
}
0