結果

問題 No.34 砂漠の行商人
ユーザー ttkkggww
提出日時 2022-03-30 02:16:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,669 ms / 5,000 ms
コード長 947 bytes
コンパイル時間 3,960 ms
コンパイル使用メモリ 253,392 KB
最終ジャッジ日時 2025-01-28 13:18:44
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int n,v,sx,sy,tx,ty;
vector<vector<int>> L;

int dp[10001][100][100];
int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};

void solve(){
	dp[0][sx][sy] = v;
	for(int i = 0;i<10000;i++){
		for(int j = 0 ;j<n;j++){
			for(int k = 0;k<n;k++){
				for(int l = 0;l<4;l++){
					int nj = j+dx[l];
					int nk = k+dy[l];
					if(nj<0||nj>=n||nk<0||nk>=n)continue;
					dp[i+1][nj][nk] = max(dp[i+1][nj][nk],dp[i][j][k]-L[nj][nk]);
				}
			}
		}
	}
	for(int i = 0;i<10000;i++){
		if(dp[i][tx][ty]){
			cout<<i<<endl;
			return;
		}
	}
	cout<<-1<<endl;

}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> v >> sx >> sy >> tx >> ty;
	L = vector<vector<int>>(n,vector<int>(n));
	swap(sx,sy);
	swap(tx,ty);
	sx--;
	sy--;
	tx--;
	ty--;
	for(int i = 0 ;i< n;i++){
		for(int j = 0;j<n;j++){
			cin >> L[i][j];
		}
	}
	solve();
}
0