結果

問題 No.34 砂漠の行商人
ユーザー fumofumofunifumofumofuni
提出日時 2020-12-05 01:18:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 702 ms / 5,000 ms
コード長 1,935 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 208,828 KB
実行使用メモリ 76,588 KB
最終ジャッジ日時 2023-10-13 12:52:55
合計ジャッジ時間 12,519 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
75,144 KB
testcase_01 AC 21 ms
75,272 KB
testcase_02 AC 46 ms
75,216 KB
testcase_03 AC 42 ms
75,256 KB
testcase_04 AC 190 ms
75,528 KB
testcase_05 AC 260 ms
75,436 KB
testcase_06 AC 167 ms
75,308 KB
testcase_07 AC 396 ms
75,752 KB
testcase_08 AC 526 ms
75,884 KB
testcase_09 AC 393 ms
75,972 KB
testcase_10 AC 691 ms
75,372 KB
testcase_11 AC 344 ms
76,256 KB
testcase_12 AC 193 ms
75,484 KB
testcase_13 AC 702 ms
76,588 KB
testcase_14 AC 686 ms
76,524 KB
testcase_15 AC 215 ms
75,676 KB
testcase_16 AC 200 ms
75,756 KB
testcase_17 AC 426 ms
75,924 KB
testcase_18 AC 54 ms
75,220 KB
testcase_19 AC 668 ms
76,400 KB
testcase_20 AC 676 ms
76,416 KB
testcase_21 AC 699 ms
76,568 KB
testcase_22 AC 624 ms
76,204 KB
testcase_23 AC 289 ms
75,744 KB
testcase_24 AC 642 ms
76,392 KB
testcase_25 AC 388 ms
75,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=(r)-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,-1,0,1,1,-1,1,-1,0};
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;
}
struct state{
    int x,y,l;
};
int dist[101][101][1801];
int main(){
    int n,v,sx,sy,gx,gy;cin >> n >> v >> sy >> sx >> gy >> gx;
    sx--;sy--;gx--;gy--;
    vvl grid(n,vl(n));
    rep(i,n)rep(j,n)cin >> grid[i][j];
    queue<state> que;
    que.push({sx,sy,0});
    rep(i,101)rep(j,101)rep(k,1801)dist[i][j][k]=-1;
    dist[sx][sy][0]=0;
    while(!que.empty()){
        state v=que.front();que.pop();
        rep(i,4){
            int nx=dx[i]+v.x,ny=dy[i]+v.y;
            if(nx<0||ny<0||nx>=n||ny>=n)continue;
            int p=grid[nx][ny];
            if(v.l+p>=1801)continue;
            if(dist[nx][ny][v.l+p]!=-1)continue;
            dist[nx][ny][v.l+p]=dist[v.x][v.y][v.l]+1;
            que.push({nx,ny,v.l+p});
        }
    }
    int ans=inf;
    rep(i,min(v,1800)){
        if(dist[gx][gy][i]!=-1)chmin(ans,dist[gx][gy][i]);
    }
    if(ans<inf/10)cout << ans <<endl;
    else cout << -1 <<endl;
}
0