結果

問題 No.34 砂漠の行商人
ユーザー uafr_cs
提出日時 2015-09-16 19:37:03
言語 Java
(openjdk 23)
結果
MLE  
実行時間 -
コード長 1,858 bytes
コンパイル時間 2,442 ms
コンパイル使用メモリ 79,284 KB
実行使用メモリ 686,368 KB
最終ジャッジ日時 2024-07-19 06:41:52
合計ジャッジ時間 24,988 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

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 Sx = sc.nextInt() - 1;
		final int Sy = sc.nextInt() - 1;
		final int Gx = sc.nextInt() - 1;
		final int Gy = sc.nextInt() - 1;
		
		int[][] damage = new int[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				damage[i][j] = sc.nextInt();
			}
		}
		
		int[][][] min_steps = new int[N][N][V + 1];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				Arrays.fill(min_steps[i][j], Integer.MAX_VALUE);
			}
		}
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		LinkedList<Integer> hp_queue = new LinkedList<Integer>();
		min_steps[Sy][Sx][V] = 0;
		x_queue.add(Sx);
		y_queue.add(Sy);
		hp_queue.add(V);
		
		while(!hp_queue.isEmpty()){		
			final int x = x_queue.poll();
			final int y = y_queue.poll();
			final int hp = hp_queue.poll();
			
			if(x == Gx && y == Gy){
				System.out.println(min_steps[y][x][hp]);
				return;
			}
			
			for(final 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 next_hp = hp - damage[ny][nx];
				if(next_hp <= 0){ continue; }
				
				final int next_step = min_steps[y][x][hp] + 1;
				if(next_step < min_steps[ny][nx][next_hp]){
					min_steps[ny][nx][next_hp] = next_step;
					x_queue.add(nx);
					y_queue.add(ny);
					hp_queue.add(next_hp);
				}
				
			}
			
		}
		
		System.out.println(-1);
	}

}
0