結果

問題 No.34 砂漠の行商人
ユーザー k82b
提出日時 2025-10-04 19:13:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,671 ms / 5,000 ms
コード長 1,578 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 201,308 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-04 19:13:15
合計ジャッジ時間 5,286 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define REP(x,y)for(ll x=0;x<ll(y);++x)
#define rep(x,y,z)for(ll x=ll(y);x<ll(z);++x)
#define PER(x,y)for(ll x=ll(y)-1;x>=0;--x)
#define per(x,y,z)for(ll x=ll(z)-1;x>=ll(y);--x)
#define all(v)begin(v),end(v)
#define rall(v)rbegin(v),rend(v)
#define pb emplace_back
#define fi first
#define se second
#define lb(v,k)ll(lower_bound(all(v),k)-begin(v))
#define ub(v,k)ll(upper_bound(all(v),k)-begin(v))
#define uniq(v)sort(all(v)),v.erase(unique(all(v)),v.end())
#define sz(x)ll(x.size())
#define out(x)cout<<(x)<<'\n'
#define sor(v)sort(all(v))
using ll=long long;
using P=pair<ll,ll>;
using PP=tuple<ll,ll,ll>;
using PPP=tuple<ll,ll,ll,ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vb=vector<bool>;
using vvb=vector<vb>;
using vp=vector<P>;
using vvp=vector<vp>;
struct $_${$_$(){ios::sync_with_stdio(false);cin.tie(nullptr);}}$_$_$;
template<class T>inline bool chmin(T&A,T B){if(A>B){A=B;return true;}return false;}
template<class T>inline bool chmax(T&A,T B){if(A<B){A=B;return true;}return false;}
constexpr int inf=1001001001;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int main()
{
	int N,V,sy,sx,gy,gx;
	cin>>N>>V>>sy>>sx>>gy>>gx;
	--sx;
	--sy;
	--gx;
	--gy;
	vvi L(N,vi(N));
	REP(i,N)REP(j,N)cin>>L[i][j];
	vvi dp(N,vi(N,-inf));
	dp[sx][sy]=V;
	REP(i,N*N)
	{
		if(dp[gx][gy]>0)
		{
			out(i);
			return 0;
		}
		vvi ep(N,vi(N,-inf));
		REP(x,N)REP(y,N)REP(j,4)
		{
			int nx=x+dx[j];
			int ny=y+dy[j];
			if(nx<0||ny<0||nx>=N||ny>=N)continue;
			chmax(ep[nx][ny],dp[x][y]-L[nx][ny]);
		}
		dp=move(ep);
	}
	out(-1);
}
0