結果

問題 No.157 2つの空洞
ユーザー kou6839kou6839
提出日時 2015-03-05 00:06:00
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,020 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 75,992 KB
実行使用メモリ 80,368 KB
最終ジャッジ日時 2023-09-06 13:24:26
合計ジャッジ時間 9,240 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
56,532 KB
testcase_01 AC 120 ms
55,912 KB
testcase_02 AC 120 ms
55,900 KB
testcase_03 AC 118 ms
56,000 KB
testcase_04 AC 120 ms
56,428 KB
testcase_05 AC 121 ms
56,180 KB
testcase_06 AC 120 ms
56,336 KB
testcase_07 AC 120 ms
55,796 KB
testcase_08 AC 123 ms
55,792 KB
testcase_09 AC 123 ms
55,640 KB
testcase_10 AC 121 ms
56,080 KB
testcase_11 AC 122 ms
55,656 KB
testcase_12 AC 125 ms
55,972 KB
testcase_13 AC 123 ms
55,980 KB
testcase_14 AC 141 ms
56,392 KB
testcase_15 AC 160 ms
57,824 KB
testcase_16 AC 125 ms
56,024 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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]);
							}
						}
					}
					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