結果

問題 No.20 砂漠のオアシス
ユーザー uafr_csuafr_cs
提出日時 2015-09-14 02:50:16
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 1,981 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 79,880 KB
実行使用メモリ 663,256 KB
最終ジャッジ日時 2024-04-21 06:43:45
合計ジャッジ時間 15,156 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
40,184 KB
testcase_01 AC 124 ms
41,492 KB
testcase_02 AC 137 ms
42,236 KB
testcase_03 AC 241 ms
52,376 KB
testcase_04 AC 427 ms
70,292 KB
testcase_05 AC 361 ms
51,900 KB
testcase_06 MLE -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static final int[][] move_dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int V = sc.nextInt();
		final int Ox = sc.nextInt() - 1;
		final int Oy = sc.nextInt() - 1;
		
		int[][] Lyx = new int[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				Lyx[i][j] = sc.nextInt();
			}
		}
		
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		LinkedList<Integer> hp_queue = new LinkedList<Integer>();
		LinkedList<Integer> oasis_queue = new LinkedList<Integer>();
		boolean[][][][] visited = new boolean[N][N][(V + 1) * 2][2];
		visited[0][0][V][0] = true;
		x_queue.add(0);
		y_queue.add(0);
		hp_queue.add(V);
		oasis_queue.add(0);
		
		boolean ok = false;
		while(!x_queue.isEmpty()){
			final int x = x_queue.poll();
			final int y = y_queue.poll();
			final int hp = hp_queue.poll();
			final int oasis = oasis_queue.poll();
			
			if(x == N - 1 && y == N - 1){
				ok = true;
				break;
			}
			
			for(int[] move : move_dir){
				final int nx = x + move[0];
				final int ny = y + move[1];
				
				if(nx < 0 || nx >= N || ny < 0 || ny >= N){
					continue;
				}
				
				final int n_hp = hp - Lyx[ny][nx];
				if(n_hp <= 0){ continue; }
				
				if(!visited[ny][nx][n_hp][oasis]){
					visited[ny][nx][n_hp][oasis] = true;
					x_queue.add(nx);
					y_queue.add(ny);
					hp_queue.add(n_hp);
					oasis_queue.add(oasis);
				}
				
				if(oasis == 0 && Ox == nx && Oy == ny && !visited[ny][nx][n_hp * 2][1]){
					visited[ny][nx][n_hp * 2][1] = true;
					x_queue.add(nx);
					y_queue.add(ny);
					hp_queue.add(n_hp * 2);
					oasis_queue.add(1);
				}
			}
		}
		
		System.out.println(ok ? "YES" : "NO");
		
	}

}
0