結果

問題 No.34 砂漠の行商人
ユーザー fiordfiord
提出日時 2015-07-22 09:36:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 675 ms / 5,000 ms
コード長 1,113 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 151,888 KB
実行使用メモリ 75,236 KB
最終ジャッジ日時 2023-09-10 19:10:25
合計ジャッジ時間 5,397 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
73,660 KB
testcase_01 AC 25 ms
73,744 KB
testcase_02 AC 26 ms
73,948 KB
testcase_03 AC 25 ms
73,880 KB
testcase_04 AC 33 ms
74,072 KB
testcase_05 AC 32 ms
73,764 KB
testcase_06 AC 26 ms
73,968 KB
testcase_07 AC 36 ms
73,748 KB
testcase_08 AC 40 ms
73,828 KB
testcase_09 AC 205 ms
74,456 KB
testcase_10 AC 38 ms
73,856 KB
testcase_11 AC 358 ms
74,964 KB
testcase_12 AC 30 ms
73,864 KB
testcase_13 AC 675 ms
75,236 KB
testcase_14 AC 500 ms
75,104 KB
testcase_15 AC 27 ms
74,032 KB
testcase_16 AC 53 ms
74,088 KB
testcase_17 AC 26 ms
73,736 KB
testcase_18 AC 28 ms
73,836 KB
testcase_19 AC 158 ms
74,708 KB
testcase_20 AC 270 ms
75,024 KB
testcase_21 AC 27 ms
73,704 KB
testcase_22 AC 30 ms
73,836 KB
testcase_23 AC 27 ms
73,740 KB
testcase_24 AC 341 ms
75,116 KB
testcase_25 AC 43 ms
74,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int dp[100][100][1800];
int mx[]={1,-1,0,0},my[]={0,0,1,-1};
int n;
bool check(int x,int y){
	if(0>x||n<=x)	return false;
	if(0>y||n<=y)	return false;
	return true;
}
int main(){
	int v,sx,sy,gx,gy;	cin>>n>>v>>sx>>sy>>gx>>gy;
	sx--;	sy--;	gx--;	gy--;
	int data[n][n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++)	cin>>data[i][j];
	}
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			for(int k=0;k<1800;k++)	dp[i][j][k]=1<<30;
		}
	}
	dp[sy][sx][0]=0;
	queue<tuple<int,int,int> > q;
	q.push(make_tuple(sx,sy,0));
	while(!q.empty()){
		auto now=q.front();	q.pop();
		int nx=get<0>(now),ny=get<1>(now),cost=get<2>(now);
		if(nx==gx&&ny==gy){
			cout<<dp[ny][nx][cost]<<endl;
			return 0;
		}
		for(int i=0;i<4;i++){
			if(check(nx+mx[i],ny+my[i])){
				if(cost+data[ny+my[i]][nx+mx[i]]>=v)	continue;
				int nex=nx+mx[i],ney=ny+my[i];
				if(dp[ney][nex][cost+data[ney][nex]]>dp[ny][nx][cost]+1){
					dp[ney][nex][cost+data[ney][nex]]=dp[ny][nx][cost]+1;
					q.push(make_tuple(nex,ney,cost+data[ney][nex]));
				}
			}
		}
	}
	cout<<-1<<endl;
	return 0;
}
						
0