結果

問題 No.34 砂漠の行商人
ユーザー oxyshoweroxyshower
提出日時 2019-02-28 13:55:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 825 ms / 5,000 ms
コード長 1,206 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 173,248 KB
実行使用メモリ 162,244 KB
最終ジャッジ日時 2023-09-05 16:08:53
合計ジャッジ時間 6,290 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,432 KB
testcase_01 AC 3 ms
7,736 KB
testcase_02 AC 8 ms
34,296 KB
testcase_03 AC 8 ms
30,284 KB
testcase_04 AC 25 ms
79,784 KB
testcase_05 AC 27 ms
92,124 KB
testcase_06 AC 16 ms
71,452 KB
testcase_07 AC 36 ms
112,720 KB
testcase_08 AC 45 ms
133,248 KB
testcase_09 AC 235 ms
123,284 KB
testcase_10 AC 55 ms
160,024 KB
testcase_11 AC 469 ms
162,024 KB
testcase_12 AC 23 ms
81,996 KB
testcase_13 AC 825 ms
162,244 KB
testcase_14 AC 617 ms
159,696 KB
testcase_15 AC 23 ms
86,740 KB
testcase_16 AC 50 ms
85,276 KB
testcase_17 AC 30 ms
121,604 KB
testcase_18 AC 12 ms
39,088 KB
testcase_19 AC 191 ms
154,140 KB
testcase_20 AC 331 ms
158,832 KB
testcase_21 AC 34 ms
157,632 KB
testcase_22 AC 40 ms
148,588 KB
testcase_23 AC 27 ms
101,476 KB
testcase_24 AC 431 ms
155,248 KB
testcase_25 AC 43 ms
117,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long

int dx[]{1,0,-1,0},dy[]{0,1,0,-1};
int board[100][100];
int dp[100][100][2001]; //{y,x,HP} = 移動回数

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,V,Sx,Sy,Gx,Gy; cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
  Sx--,Sy--,Gx--,Gy--;
  for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      cin >> board[i][j];
    }
  }

  V = min(V,2000LL);
  for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      fill(dp[i][j],dp[i][j]+V+1,-1);
    }
  }
  dp[Sy][Sx][V] = 0;
  queue< tuple<int,int,int> > q;
  q.push({Sy,Sx,V});
  while(!q.empty()){
    auto p = q.front(); q.pop();
    int y = get<0>(p);
    int x = get<1>(p);
    int v = get<2>(p);
    for(int i = 0; i < 4; i++){
      int ny = y + dy[i];
      int nx = x + dx[i];
      if(ny < 0 || N <= ny || nx < 0 || N <= nx) continue;
      int nv = v - board[ny][nx];
      if(nv <= 0) continue;
      if(dp[ny][nx][nv] != -1) continue;
      dp[ny][nx][nv] = dp[y][x][v] + 1;
      if(ny == Gy && nx == Gx){
        cout << dp[ny][nx][nv] << endl;
        return 0;
      }
      q.push({ny,nx,nv});
    }
  }
  cout << -1 << endl;

  return 0;
}
0