結果

問題 No.157 2つの空洞
ユーザー ぴろずぴろず
提出日時 2015-02-26 23:42:59
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 76,432 KB
実行使用メモリ 56,276 KB
最終ジャッジ日時 2023-09-06 02:48:44
合計ジャッジ時間 6,049 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,668 KB
testcase_01 AC 122 ms
55,832 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 125 ms
55,780 KB
testcase_06 AC 124 ms
56,168 KB
testcase_07 AC 119 ms
55,756 KB
testcase_08 AC 121 ms
55,908 KB
testcase_09 AC 121 ms
55,712 KB
testcase_10 AC 118 ms
55,936 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 125 ms
56,176 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 125 ms
56,276 KB
testcase_18 WA -
testcase_19 AC 125 ms
55,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no157;

import java.util.ArrayDeque;
import java.util.Scanner;

public class Main {
	static int[] di = {1,0,-1,0};
	static int[] dj = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int h = sc.nextInt();
		int[][] c = new int[h][w];
		int si = -1;
		int sj = -1;
		for(int i=0;i<h;i++) {
			String s = sc.next();
			for(int j=0;j<w;j++) {
				c[i][j] = s.charAt(j) == '#' ? 1 : 0;
				if (c[i][j] == 0 && si == -1) {
					si = i;
					sj = j;
				}
			}
		}

		int[][] dist = new int[h][w];
		for(int i=0;i<h;i++) {
			for(int j=0;j<w;j++) {
				dist[i][j] = 1<<29;
			}
		}
		dist[si][sj] = 0;
		ArrayDeque<Point> q = new ArrayDeque<>();
		q.offer(new Point(si,sj));
		int ans = 1 << 29;
		while(!q.isEmpty()) {
			Point p = q.pollFirst();
//			System.out.println(p);
			for(int i=0;i<4;i++) {
				int ni = p.i + di[i];
				int nj = p.j + dj[i];
				if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
					continue;
				}
				int d = dist[p.i][p.j] + c[ni][nj];
				if (d < dist[ni][nj]) {
					dist[ni][nj] = d;
					if (c[p.i][p.j] == 1 && c[ni][nj] == 0 && dist[ni][nj] != 0) {
						ans = Math.min(ans,d);
					}else{
						q.offer(new Point(ni,nj));
					}
				}
			}
		}
		System.out.println(ans);
	}
	static class Point {
		int i,j;
		public Point(int i,int j) {
			this.i = i;
			this.j = j;
		}
		public String toString() {
			return i + "," + j;
		}
	}
}
0