結果

問題 No.34 砂漠の行商人
ユーザー htensaihtensai
提出日時 2020-06-12 18:32:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 578 ms / 5,000 ms
コード長 1,996 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 74,728 KB
実行使用メモリ 67,568 KB
最終ジャッジ日時 2023-09-06 09:22:12
合計ジャッジ時間 6,997 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,608 KB
testcase_01 AC 46 ms
49,484 KB
testcase_02 AC 63 ms
52,100 KB
testcase_03 AC 54 ms
49,704 KB
testcase_04 AC 98 ms
51,532 KB
testcase_05 AC 94 ms
51,472 KB
testcase_06 AC 110 ms
53,556 KB
testcase_07 AC 129 ms
55,052 KB
testcase_08 AC 141 ms
55,572 KB
testcase_09 AC 129 ms
55,136 KB
testcase_10 AC 578 ms
67,568 KB
testcase_11 AC 120 ms
53,996 KB
testcase_12 AC 79 ms
51,820 KB
testcase_13 AC 193 ms
56,652 KB
testcase_14 AC 186 ms
56,316 KB
testcase_15 AC 95 ms
52,096 KB
testcase_16 AC 94 ms
51,568 KB
testcase_17 AC 119 ms
53,972 KB
testcase_18 AC 76 ms
51,052 KB
testcase_19 AC 158 ms
55,268 KB
testcase_20 AC 176 ms
56,020 KB
testcase_21 AC 87 ms
51,092 KB
testcase_22 AC 158 ms
55,548 KB
testcase_23 AC 116 ms
54,032 KB
testcase_24 AC 178 ms
55,620 KB
testcase_25 AC 129 ms
54,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 6);
		int n = Integer.parseInt(first[0]);
		int v = Integer.parseInt(first[1]);
		int sc = Integer.parseInt(first[2]) - 1;
		int sr = Integer.parseInt(first[3]) - 1;
		int gc = Integer.parseInt(first[4]) - 1;
		int gr = Integer.parseInt(first[5]) - 1;
		int[][] field = new int[n][n];
		int[][] costs = new int[n][n];
		for (int i = 0; i < n; i++) {
		    String[] line = br.readLine().split(" ", n);
		    for (int j = 0; j < n; j++) {
		        field[i][j] = line[j].charAt(0) - '0';
		    }
		    Arrays.fill(costs[i], Integer.MAX_VALUE);
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(sr, sc, -field[sr][sc], 0));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (p.row < 0 || p.row >= n || p.col < 0 || p.col >= n) {
		        continue;
		    }
		    p.value += field[p.row][p.col];
		    if (p.value >= v || costs[p.row][p.col] <= p.count) {
		        continue;
		    }
		    costs[p.row][p.col] = p.count;
		    queue.add(new Path(p.row + 1, p.col, p.value, p.count + 1));
		    queue.add(new Path(p.row - 1, p.col, p.value, p.count + 1));
		    queue.add(new Path(p.row, p.col + 1, p.value, p.count + 1));
		    queue.add(new Path(p.row, p.col - 1, p.value, p.count + 1));
		}
		if (costs[gr][gc] == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(costs[gr][gc]);
		}
	}
	
	static class Path implements Comparable<Path> {
	    int row;
	    int col;
	    int value;
	    int count;
	    
	    public Path(int row, int col, int value, int count) {
	        this.row = row;
	        this.col = col;
	        this.value = value;
	        this.count = count;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}
0