結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
kou6839
|
| 提出日時 | 2015-02-20 16:25:18 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 298 ms / 5,000 ms |
| コード長 | 1,757 bytes |
| コンパイル時間 | 2,227 ms |
| コンパイル使用メモリ | 79,124 KB |
| 実行使用メモリ | 48,540 KB |
| 最終ジャッジ日時 | 2024-06-28 10:14:36 |
| 合計ジャッジ時間 | 9,166 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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);
}
}
kou6839