結果

問題 No.157 2つの空洞
ユーザー uafr_csuafr_cs
提出日時 2015-06-17 04:09:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,885 bytes
コンパイル時間 2,807 ms
コンパイル使用メモリ 74,864 KB
実行使用メモリ 56,264 KB
最終ジャッジ日時 2023-09-21 09:15:35
合計ジャッジ時間 6,418 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
53,844 KB
testcase_01 AC 125 ms
55,592 KB
testcase_02 AC 127 ms
55,968 KB
testcase_03 AC 128 ms
55,864 KB
testcase_04 AC 128 ms
55,760 KB
testcase_05 AC 128 ms
56,000 KB
testcase_06 AC 128 ms
55,940 KB
testcase_07 AC 126 ms
56,008 KB
testcase_08 AC 126 ms
55,760 KB
testcase_09 AC 128 ms
55,816 KB
testcase_10 AC 128 ms
55,784 KB
testcase_11 AC 128 ms
55,760 KB
testcase_12 AC 130 ms
55,992 KB
testcase_13 AC 130 ms
55,780 KB
testcase_14 AC 131 ms
56,264 KB
testcase_15 AC 130 ms
56,164 KB
testcase_16 AC 133 ms
56,200 KB
testcase_17 AC 136 ms
55,584 KB
testcase_18 AC 131 ms
55,520 KB
testcase_19 AC 132 ms
55,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	
	public static final int INF = Integer.MAX_VALUE / 2 - 1;
	
	public static final int[] vs = {1, 0, -1, 0};
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int W = sc.nextInt();
		final int H = sc.nextInt();
		
		boolean[][] is_wall = new boolean[H][W];
		
		for(int i = 0; i < H; i++){
			char[] inputs = sc.next().toCharArray();
			
			for(int j = 0; j < W; j++){
				is_wall[i][j] = inputs[j] == '#';
			}
		}
		
		int sx = -1, sy = -1;
		LOOP:
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(!is_wall[i][j]){
					sx = j; sy = i;
					break LOOP;
				}
			}
		}
		
		int[][] dists = new int[H][W];
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				dists[i][j] = INF;
			}
		}
		dists[sy][sx] = 0;
		
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		y_queue.add(sy);
		x_queue.add(sx);
		
		while(!y_queue.isEmpty()){
			final int y = y_queue.poll();
			final int x = x_queue.poll();
			
			if(dists[y][x] != 0 && !is_wall[y][x]){
				System.out.println(dists[y][x]);
				break;
			}
			
			for(int v = 0; v < vs.length; v++){
				final int nx = x + vs[v];
				final int ny = y + vs[(v + 1) % vs.length];
				
				if(nx < 0 || nx >= W || ny < 0 || ny >= H){
					continue;
				}
				
				if(is_wall[ny][nx] && dists[ny][nx] > dists[y][x] + 1){
					dists[ny][nx] = Math.min(dists[ny][nx], dists[y][x] + 1);
					y_queue.addLast(ny);
					x_queue.addLast(nx);
				}
				
				if(!is_wall[ny][nx] && dists[ny][nx] > dists[y][x]){
					dists[ny][nx] = Math.min(dists[ny][nx], dists[y][x]);
					y_queue.addFirst(ny);
					x_queue.addFirst(nx);
				}
			}
		}
		
		
		
	}
}
0