結果

問題 No.34 砂漠の行商人
ユーザー beetbeet
提出日時 2019-03-30 18:02:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 993 ms / 5,000 ms
コード長 1,543 bytes
コンパイル時間 2,062 ms
コンパイル使用メモリ 204,548 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-09 22:42:13
合計ジャッジ時間 5,046 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 4 ms
4,384 KB
testcase_07 AC 450 ms
4,380 KB
testcase_08 AC 13 ms
4,380 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 25 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 31 ms
4,380 KB
testcase_14 AC 24 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 12 ms
4,376 KB
testcase_20 AC 16 ms
4,380 KB
testcase_21 AC 993 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 17 ms
4,380 KB
testcase_25 AC 6 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}


//INSERT ABOVE HERE
signed main(){
  Int n,v,sy,sx,gy,gx;
  cin>>n>>v>>sx>>sy>>gx>>gy;
  sy--;sx--;gy--;gx--;

  auto l=make_v<Int>(n,n);
  for(Int i=0;i<n;i++)
    for(Int j=0;j<n;j++)
      cin>>l[i][j];

  auto dp=make_v<Int>(n,n);
  fill_v<Int>(dp,0);
  dp[sy][sx]=v;
  
  for(Int t=0;t<=n*n;t++){
    if(dp[gy][gx]>0){
      cout<<t<<endl;
      return 0;
    }
    
    auto nx=make_v<Int>(n,n);
    fill_v<Int>(nx,0);

    Int dy[]={0,0,1,-1};
    Int dx[]={1,-1,0,0};
    auto in=[&](Int y,Int x){return 0<=y&&y<n&&0<=x&&x<n;};
    
    for(Int i=0;i<n;i++){
      for(Int j=0;j<n;j++){
        for(Int k=0;k<4;k++){
          Int ni=i+dy[k],nj=j+dx[k];
          if(in(ni,nj)) chmax(nx[ni][nj],dp[i][j]-l[ni][nj]);
        }
      }      
    }
    
    swap(dp,nx);
  }
  cout<<-1<<endl;
  return 0;
}
0