結果

問題 No.34 砂漠の行商人
ユーザー htensaihtensai
提出日時 2020-05-11 16:54:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,958 bytes
コンパイル時間 5,190 ms
コンパイル使用メモリ 74,536 KB
実行使用メモリ 62,128 KB
最終ジャッジ日時 2023-09-25 17:29:27
合計ジャッジ時間 11,904 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,376 KB
testcase_01 AC 126 ms
55,700 KB
testcase_02 AC 164 ms
56,108 KB
testcase_03 AC 157 ms
56,296 KB
testcase_04 AC 247 ms
60,160 KB
testcase_05 AC 260 ms
60,152 KB
testcase_06 WA -
testcase_07 AC 306 ms
59,988 KB
testcase_08 AC 325 ms
59,988 KB
testcase_09 AC 299 ms
59,992 KB
testcase_10 AC 531 ms
62,128 KB
testcase_11 AC 300 ms
60,232 KB
testcase_12 AC 230 ms
59,180 KB
testcase_13 AC 379 ms
60,132 KB
testcase_14 AC 392 ms
60,352 KB
testcase_15 AC 242 ms
60,064 KB
testcase_16 AC 232 ms
59,228 KB
testcase_17 AC 282 ms
60,576 KB
testcase_18 AC 190 ms
56,564 KB
testcase_19 AC 352 ms
60,232 KB
testcase_20 AC 383 ms
60,132 KB
testcase_21 AC 232 ms
59,252 KB
testcase_22 AC 322 ms
59,764 KB
testcase_23 AC 280 ms
60,176 KB
testcase_24 AC 358 ms
59,904 KB
testcase_25 AC 301 ms
60,804 KB
権限があれば一括ダウンロードができます

ソースコード

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];
		int[][] counts = new int[n][n];
		ArrayList<ArrayList<TreeMap<Integer, Integer>>> values = new ArrayList<>();
		for (int i = 0; i < n; i++) {
		    Arrays.fill(counts[i], Integer.MAX_VALUE);
		    for (int j = 0; j < n; j++) {
		        costs[i][j] = sc.nextInt();
		    }
		}
		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 (counts[p.y][p.x] <= p.count) {
		        continue;
		    }
		    counts[p.y][p.x] = 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));
		    }
		}
		if (counts[gy][gx] == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(counts[gy][gx]);
		}
	}
	
	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