結果

問題 No.34 砂漠の行商人
ユーザー uafr_csuafr_cs
提出日時 2015-09-16 21:09:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 233 ms / 5,000 ms
コード長 2,733 bytes
コンパイル時間 4,543 ms
コンパイル使用メモリ 74,452 KB
実行使用メモリ 55,196 KB
最終ジャッジ日時 2023-09-10 19:16:16
合計ジャッジ時間 6,309 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,276 KB
testcase_01 AC 43 ms
49,400 KB
testcase_02 AC 50 ms
49,448 KB
testcase_03 AC 46 ms
49,380 KB
testcase_04 AC 113 ms
52,732 KB
testcase_05 AC 111 ms
52,640 KB
testcase_06 AC 100 ms
53,168 KB
testcase_07 AC 120 ms
55,072 KB
testcase_08 AC 131 ms
53,196 KB
testcase_09 AC 140 ms
53,152 KB
testcase_10 AC 174 ms
53,500 KB
testcase_11 AC 233 ms
55,128 KB
testcase_12 AC 90 ms
52,980 KB
testcase_13 AC 215 ms
55,196 KB
testcase_14 AC 156 ms
53,132 KB
testcase_15 AC 79 ms
51,680 KB
testcase_16 AC 91 ms
52,348 KB
testcase_17 AC 80 ms
51,988 KB
testcase_18 AC 52 ms
50,580 KB
testcase_19 AC 128 ms
53,432 KB
testcase_20 AC 166 ms
53,144 KB
testcase_21 AC 130 ms
52,828 KB
testcase_22 AC 125 ms
52,380 KB
testcase_23 AC 95 ms
52,764 KB
testcase_24 AC 182 ms
53,520 KB
testcase_25 AC 102 ms
53,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

	public static final int[][] move_dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
	
	public static void main(String[] args) throws IOException {
		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[][] cur_max_hp = new int[N][N];
		int[][] next_max_hp = new int[N][N];
		
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				cur_max_hp[i][j] = 0;
				next_max_hp[i][j] = 0;
			}
		}
		
		int count = 0;
		cur_max_hp[Sy][Sx] = V;
		while(true){
			for(int i = 0; i < N; i++){
				for(int j = 0; j < N; j++){
					next_max_hp[i][j] = cur_max_hp[i][j];
				}
			}
			
			boolean updated = false;
			
			for(int x = 0; x < N; x++){
				for(int y = 0; y < N; y++){
					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 next_hp = cur_max_hp[y][x] - damage[ny][nx];
						
						if(next_hp > next_max_hp[ny][nx]){
							updated = true;
							next_max_hp[ny][nx] = next_hp;
						}
					}
				}
			}
				
			if(next_max_hp[Gy][Gx] > 0){
				System.out.println(count + 1);
				break;
			}else if(!updated){
				System.out.println(-1);
				break;
			}
			
			count++;
			
			int[][] tmp = cur_max_hp;
			cur_max_hp = next_max_hp;
			next_max_hp = tmp;
		}
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public void close() throws IOException {
			br.close();
		}
	}

}
0