結果

問題 No.34 砂漠の行商人
ユーザー htensaihtensai
提出日時 2020-05-11 16:04:49
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,089 bytes
コンパイル時間 3,495 ms
コンパイル使用メモリ 77,116 KB
実行使用メモリ 95,520 KB
最終ジャッジ日時 2023-09-25 16:02:47
合計ジャッジ時間 18,406 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,784 KB
testcase_01 AC 133 ms
56,016 KB
testcase_02 AC 233 ms
57,976 KB
testcase_03 AC 299 ms
59,624 KB
testcase_04 AC 993 ms
78,624 KB
testcase_05 AC 885 ms
75,664 KB
testcase_06 WA -
testcase_07 AC 1,286 ms
83,236 KB
testcase_08 AC 1,672 ms
95,520 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int v = sc.nextInt();
		int sx = sc.nextInt() - 1;
		int sy = sc.nextInt() - 1;
		int gx = sc.nextInt() - 1;
		int gy = sc.nextInt() - 1;
		int[][] costs = new int[n][n];
		ArrayList<ArrayList<HashMap<Integer, Integer>>> values = new ArrayList<>();
		for (int i = 0; i < n; i++) {
		    values.add(new ArrayList<>());
		    for (int j = 0; j < n; j++) {
		        costs[i][j] = sc.nextInt();
		        values.get(i).add(new HashMap<>());
		    }
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(sx, sy, 0, v));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    p.value -= costs[p.y][p.x];
		    if (p.value <= 0) {
		        continue;
		    }
		    if (values.get(p.y).get(p.x).containsKey(p.value)) {
		        continue;
		    }
		    values.get(p.y).get(p.x).put(p.value, p.count);
		    if (p.x > 0) {
		        queue.add(new Path(p.x - 1, p.y, p.count + 1, p.value));
		    }
		    if (p.x < n - 1) {
		        queue.add(new Path(p.x + 1, p.y, p.count + 1, p.value));
		    }
		    if (p.y > 0) {
		        queue.add(new Path(p.x, p.y - 1, p.count + 1, p.value));
		    }
		    if (p.y < n - 1) {
		        queue.add(new Path(p.x, p.y + 1, p.count + 1, p.value));
		    }
		}
		int min = Integer.MAX_VALUE;
		for (int x : values.get(gy).get(gx).values()) {
		    min = Math.min(min, x);
		}
		if (min == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(min);
		}
	}
	
	static class Path implements Comparable<Path> {
	    int x;
	    int y;
	    int count;
	    int value;
	    
	    public Path(int x, int y, int count, int value) {
	        this.x = x;
	        this.y = y;
	        this.count = count;
	        this.value = value;
	    }
	    
	    public int compareTo(Path another) {
	        if (value == another.value) {
	            return count - another.count;
	        } else {
	            return another.value - value;
	        }
	    }
	}
}
0