結果

問題 No.34 砂漠の行商人
ユーザー maimai
提出日時 2017-03-23 08:31:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,187 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 170,400 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 19:27:15
合計ジャッジ時間 2,670 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long int ll;

using namespace std;

int mat[111][111];
int dp[111][111];
ll n,hp,ssx,ssy,ggx,ggy;


int main(){
  cin>>n>>hp>>ssx>>ssy>>ggx>>ggy;
  
  for(int y=1;y<=n;++y){
    for(int x=1;x<=n;++x){
      scanf("%d",&(mat[y][x]));
      ++mat[y][x];
    }}
  //fill(&dp[0][0],&dp[110][110]+1,101010);

  queue<ll> q;
  hp+=mat[ssy][ssx]-1;
  dp[ssy][ssx]=hp;

  q.push((ssx<<8)|(ssy)|(hp<<32));

  while(!q.empty()){
    ll d=q.front();q.pop();
    ll x=(d>>8)&0xff;
    ll y=(d)&0xff;
    ll s=(d>>16)&0xffff;
    ll h=(d>>32)&0xffff;
    
    if ((h-=mat[y][x]-1)<=0) continue;
    if (x==ggx && y==ggy){
      cout << s << endl;
      exit(0);
    }
    ++s;
    if (mat[y-1][x] && h>dp[y-1][x]){
      dp[y-1][x]=h;
      q.push((y-1)|((x)<<8)|(s<<16)|(h<<32));
    }
    if (mat[y+1][x] && h>dp[y+1][x]){
      dp[y+1][x]=h;
      q.push((y+1)|((x)<<8)|(s<<16)|(h<<32));
    }
    if (mat[y][x-1] && h>dp[y][x-1]){
      dp[y][x-1]=h;
      q.push((y)|((x-1)<<8)|(s<<16)|(h<<32));
    }
    if (mat[y][x+1] && h>dp[y][x+1]){
      dp[y][x+1]=h;
      q.push((y)|((x+1)<<8)|(s<<16)|(h<<32));
    }
  }
  
  cout<<"-1"<<endl;
  
  return 0;
}
0