結果

問題 No.34 砂漠の行商人
ユーザー 沙耶花
提出日時 2021-10-21 15:14:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,132 bytes
コンパイル時間 4,384 ms
コンパイル使用メモリ 255,184 KB
最終ジャッジ日時 2025-01-25 02:21:11
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000

int dis[101][101][10001];

int main(){
	
	int n,v,sx,sy,gx,gy;
	cin>>n>>v>>sx>>sy>>gx>>gy;
	gx--;gy--;sx--;sy--;
	swap(sx,sy);
	swap(gx,gy);
	
	vector l(n,vector<int>(n));
	rep(i,n){
		rep(j,n)cin>>l[i][j];
	}
	
	rep(i,n){
		rep(j,n){
			rep(k,v+1)dis[i][j][k] = Inf;
		}
	}
	
	queue<pair<pair<int,int>,int>> Q;
	Q.emplace(make_pair(sx,sy),v);
	dis[sx][sy][v] = 0;
	vector<int> dx = {1,-1,0,0},dy = {0,0,1,-1};
	while(Q.size()>0){
		int xx = Q.front().first.first, yy = Q.front().first.second,vv = Q.front().second;
		Q.pop();
		rep(i,4){
			int nx = xx + dx[i];
			int ny = yy + dy[i];
			if(nx<0||nx>=n||ny<0||ny>=n)continue;
			int nv = vv - l[nx][ny];
			if(nv<=0)continue;
			if(dis[nx][ny][nv]!=Inf)continue;
			dis[nx][ny][nv] = dis[xx][yy][vv] + 1;
			Q.emplace(make_pair(nx,ny),nv);
		}
	}
	
	int ans = Inf;
	rep(i,v){
		ans = min(ans,dis[gx][gy][i+1]);
	}
	if(ans==Inf)ans = -1;
	cout<<ans<<endl;
	
	return 0;
}
0