結果

問題 No.34 砂漠の行商人
ユーザー 37zigen37zigen
提出日時 2016-05-01 15:43:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 431 ms / 5,000 ms
コード長 1,681 bytes
コンパイル時間 3,892 ms
コンパイル使用メモリ 74,336 KB
実行使用メモリ 62,264 KB
最終ジャッジ日時 2023-09-10 19:24:03
合計ジャッジ時間 9,998 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,820 KB
testcase_01 AC 120 ms
55,912 KB
testcase_02 AC 171 ms
56,076 KB
testcase_03 AC 142 ms
55,228 KB
testcase_04 AC 212 ms
58,868 KB
testcase_05 AC 225 ms
59,420 KB
testcase_06 AC 195 ms
56,504 KB
testcase_07 AC 249 ms
59,648 KB
testcase_08 AC 280 ms
59,936 KB
testcase_09 AC 310 ms
60,236 KB
testcase_10 AC 265 ms
60,048 KB
testcase_11 AC 292 ms
60,200 KB
testcase_12 AC 209 ms
59,600 KB
testcase_13 AC 431 ms
62,264 KB
testcase_14 AC 428 ms
60,848 KB
testcase_15 AC 197 ms
57,716 KB
testcase_16 AC 231 ms
59,952 KB
testcase_17 AC 208 ms
58,276 KB
testcase_18 AC 176 ms
56,188 KB
testcase_19 AC 331 ms
60,928 KB
testcase_20 AC 372 ms
60,276 KB
testcase_21 AC 233 ms
60,260 KB
testcase_22 AC 231 ms
58,892 KB
testcase_23 AC 205 ms
59,400 KB
testcase_24 AC 394 ms
60,040 KB
testcase_25 AC 233 ms
59,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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()-1;
		int sy=sc.nextInt()-1;
		int gx=sc.nextInt()-1;
		int gy=sc.nextInt()-1;
		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;
		}
	}
}
0