結果

問題 No.34 砂漠の行商人
ユーザー kou6839kou6839
提出日時 2015-02-20 16:25:18
言語 Java19
(openjdk 21)
結果
AC  
実行時間 285 ms / 5,000 ms
コード長 1,757 bytes
コンパイル時間 2,580 ms
コンパイル使用メモリ 74,988 KB
実行使用メモリ 61,596 KB
最終ジャッジ日時 2023-09-10 19:05:43
合計ジャッジ時間 9,766 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,856 KB
testcase_01 AC 127 ms
55,788 KB
testcase_02 AC 163 ms
56,140 KB
testcase_03 AC 154 ms
55,932 KB
testcase_04 AC 213 ms
58,440 KB
testcase_05 AC 228 ms
59,568 KB
testcase_06 AC 214 ms
58,776 KB
testcase_07 AC 253 ms
61,148 KB
testcase_08 AC 262 ms
61,284 KB
testcase_09 AC 251 ms
60,980 KB
testcase_10 AC 264 ms
60,152 KB
testcase_11 AC 265 ms
60,868 KB
testcase_12 AC 209 ms
58,964 KB
testcase_13 AC 285 ms
61,156 KB
testcase_14 AC 282 ms
60,756 KB
testcase_15 AC 207 ms
57,240 KB
testcase_16 AC 214 ms
59,072 KB
testcase_17 AC 208 ms
58,984 KB
testcase_18 AC 191 ms
56,876 KB
testcase_19 AC 270 ms
61,456 KB
testcase_20 AC 279 ms
60,908 KB
testcase_21 AC 238 ms
59,868 KB
testcase_22 AC 241 ms
60,108 KB
testcase_23 AC 208 ms
58,728 KB
testcase_24 AC 274 ms
61,596 KB
testcase_25 AC 231 ms
59,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.*;
import java.util.*;

public class Main {
	class pair{
		int num;
		int cost;
		pair(int num,int cost){
			this.num=num;
			this.cost=cost;
		}
	}
    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[][] map = new int[n][n];
        for(int i=0;i<n;i++){
        	for(int j=0;j<n;j++){
        		map[i][j]=sc.nextInt();
        	}
        }
        int[] dx = {1,0,-1,0};
        int[] dy = {0,1,0,-1};
        
        int[][] totalcost = new int[n][n];
        for(int i=0;i<n;i++) Arrays.fill(totalcost[i], 1000000);
        Queue<Integer> queuex = new LinkedList<Integer>();
        Queue<Integer> queuey = new LinkedList<Integer>();
        Queue<Integer> hosuu = new LinkedList<Integer>();
		totalcost[sy][sx]=0;
        queuex.offer(sx);
        queuey.offer(sy);
        hosuu.offer(0);
        while(!queuex.isEmpty()){
        	int nx = queuex.poll();
        	int ny = queuey.poll();
        	int nh = hosuu.poll();
        	if(nx==gx && ny == gy){
        		System.out.println(nh);
        		return;
        	}
        	for(int i=0;i<4;i++){
        		if(nx+dx[i]>=0 && nx+dx[i]<n && ny+dy[i]>=0 && ny+dy[i]<n &&totalcost[ny][nx]+map[ny+dy[i]][nx+dx[i]]<v&& totalcost[ny][nx]+map[ny+dy[i]][nx+dx[i]]<totalcost[ny+dy[i]][nx+dx[i]]){
        			queuex.offer(nx+dx[i]);
        			queuey.offer(ny+dy[i]);
        			hosuu.offer(nh+1);
        			totalcost[ny+dy[i]][nx+dx[i]]=totalcost[ny][nx]+map[ny+dy[i]][nx+dx[i]];
        		}
        	}
        }
        System.out.println(-1);
    }
}			
0