結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-05-01 15:30:00 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,673 bytes |
| コンパイル時間 | 2,270 ms |
| コンパイル使用メモリ | 77,888 KB |
| 実行使用メモリ | 48,680 KB |
| 最終ジャッジ日時 | 2024-10-05 00:35:37 |
| 合計ジャッジ時間 | 8,185 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 WA * 2 RE * 8 |
ソースコード
package yukicoder;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class Main{
public static void main(String[] args)throws Exception{
new Main().solve();
}
int V;
void solve(){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
V=sc.nextInt();
int sx=sc.nextInt();
int sy=sc.nextInt();
int gx=sc.nextInt();
int gy=sc.nextInt();
int[][] level=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
level[i][j]=sc.nextInt();
}
}
BFS bfs=new BFS(level);
int ans=bfs.bfs(sx, sy, gx, gy);
System.out.println(ans);
}
int[] dx={1,-1,0,0};
int[] dy={0,0,-1,1};
class BFS{
Queue<Vertice> q;
final long INF=Long.MAX_VALUE/4;
int[][] table;
int w,h;
int[][] arrived;
BFS(int[][] table){
h=table.length;
w=table[0].length;
this.table=table;
q=new ArrayDeque<Vertice>(h*w);
arrived=new int[h][w];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
arrived[i][j]=0;
}
}
}
int bfs(int sx,int sy,int gx,int gy){
q.add(new Vertice(sx,sy,0,V));
arrived[sy][sx]=V;
int ans=-1;
while(!q.isEmpty()){
Vertice v=q.poll();
if(v.x==gx&&v.y==gy){
ans=v.d;
break;
}
for(int i=0;i<4;i++){
int nx=v.x+dx[i];
int ny=v.y+dy[i];
if(nx<0||ny<0||nx>=w||ny>=h)continue;
if(arrived[ny][nx]>=v.hp-table[ny][nx])continue;
arrived[ny][nx]=v.hp-table[ny][nx];
q.add(new Vertice(nx, ny, v.d+1,v.hp-table[ny][nx]));
}
}
return ans;
}
}
class Vertice{
int x;
int y;
int d;
int hp;
Vertice(int x,int y,int d,int hp){
this.x=x;
this.y=y;
this.d=d;
this.hp=hp;
}
}
}