結果

問題 No.34 砂漠の行商人
ユーザー scachescache
提出日時 2014-12-01 14:07:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 474 ms / 5,000 ms
コード長 1,704 bytes
コンパイル時間 3,832 ms
コンパイル使用メモリ 75,428 KB
実行使用メモリ 67,188 KB
最終ジャッジ日時 2023-09-10 18:55:30
合計ジャッジ時間 11,692 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,792 KB
testcase_01 AC 121 ms
55,992 KB
testcase_02 AC 173 ms
56,128 KB
testcase_03 AC 146 ms
55,768 KB
testcase_04 AC 221 ms
59,144 KB
testcase_05 AC 233 ms
59,468 KB
testcase_06 AC 197 ms
58,868 KB
testcase_07 AC 266 ms
59,784 KB
testcase_08 AC 285 ms
60,376 KB
testcase_09 AC 328 ms
60,500 KB
testcase_10 AC 260 ms
59,948 KB
testcase_11 AC 276 ms
60,408 KB
testcase_12 AC 213 ms
59,604 KB
testcase_13 AC 474 ms
67,188 KB
testcase_14 AC 443 ms
64,956 KB
testcase_15 AC 192 ms
58,684 KB
testcase_16 AC 230 ms
60,068 KB
testcase_17 AC 194 ms
59,276 KB
testcase_18 AC 177 ms
55,968 KB
testcase_19 AC 367 ms
60,328 KB
testcase_20 AC 445 ms
61,040 KB
testcase_21 AC 233 ms
58,020 KB
testcase_22 AC 232 ms
57,996 KB
testcase_23 AC 198 ms
58,900 KB
testcase_24 AC 426 ms
62,588 KB
testcase_25 AC 231 ms
60,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main34 {
	public static void main(String[] args) {
		Main34 p = new Main34();
	}

	public Main34(){
		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[][] l = new int[n][n];
		for(int i=0;i<l.length;i++){
			for(int j=0;j<l[i].length;j++)
				l[i][j] = sc.nextInt();
		}
		
		System.out.println(solve(v, sx, sy, gx, gy, l));
	}

	int[] dx = { -1, 0, 1, 0};
	int[] dy = { 0, -1, 0, 1};
	private int solve(int v, int sx, int sy, int gx, int gy, int[][] l) {
		int[][] tl = new int[l.length][l.length];
		for (int i = 0; i < tl.length; i++) {
			Arrays.fill(tl[i], v+1);
		}
		
		LinkedList<Point> queue = new LinkedList<>();
		queue.add(new Point(sx, sy, 0, 0));
				
		while(!queue.isEmpty()){
			Point p = queue.poll();
			
			for(int i=0;i<4;i++){
				int nx = dx[i] + p.x;
				int ny = dy[i] + p.y;
				
				if(nx<0 || l.length<=nx || ny <0 || l.length<=ny)
					continue;
				
				if(nx == gx && ny == gy && v>p.totall+l[ny][nx])
					return p.dist+1;
				
				if(tl[ny][nx] > p.totall+l[ny][nx]){
					tl[ny][nx] = p.totall+l[ny][nx];
					queue.add(new Point(nx, ny, p.totall+l[ny][nx], p.dist+1));
				}
			}
		}
		
		return -1;
		
		
	}

	private class Point{
		int x;
		int y;
		int totall;
		int dist;
		
		public Point(int x, int y, int totall, int dist) {
			this.x = x;
			this.y = y;
			this.totall = totall;
			this.dist = dist;
		}
		
		
	}
	
}
0