結果

問題 No.34 砂漠の行商人
ユーザー purple_jwlpurple_jwl
提出日時 2015-03-21 01:14:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 753 ms / 5,000 ms
コード長 1,564 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 148,660 KB
実行使用メモリ 82,860 KB
最終ジャッジ日時 2023-09-10 19:07:03
合計ジャッジ時間 5,855 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
5,648 KB
testcase_02 AC 5 ms
18,052 KB
testcase_03 AC 4 ms
15,924 KB
testcase_04 AC 17 ms
41,040 KB
testcase_05 AC 18 ms
47,452 KB
testcase_06 AC 9 ms
36,724 KB
testcase_07 AC 26 ms
57,588 KB
testcase_08 AC 33 ms
68,108 KB
testcase_09 AC 218 ms
63,332 KB
testcase_10 AC 48 ms
81,980 KB
testcase_11 AC 427 ms
82,560 KB
testcase_12 AC 16 ms
43,104 KB
testcase_13 AC 753 ms
82,860 KB
testcase_14 AC 575 ms
82,576 KB
testcase_15 AC 17 ms
45,788 KB
testcase_16 AC 44 ms
44,032 KB
testcase_17 AC 21 ms
62,336 KB
testcase_18 AC 9 ms
20,480 KB
testcase_19 AC 167 ms
79,676 KB
testcase_20 AC 295 ms
80,360 KB
testcase_21 AC 20 ms
80,236 KB
testcase_22 AC 29 ms
77,104 KB
testcase_23 AC 19 ms
52,056 KB
testcase_24 AC 386 ms
80,256 KB
testcase_25 AC 32 ms
59,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

struct State {
  int x, y, damage;
  State(int x, int y, int d): x(x), y(y), damage(d) {}
};

int N, V, SX, SY, GX, GY;
int L[100][100];
int minCost[100][100][2000];

int solve() {
  SX--;
  SY--;
  GX--;
  GY--;
  V = min(2000, V);
  rep(i, N) rep(j, N) rep(k, V) minCost[i][j][k] = 1 << 29;
  
  queue<State> Q;
  Q.push(State(SX, SY, 0));
  minCost[SY][SX][0] = 0;

  while(!Q.empty()) {
    State stt = Q.front();
    Q.pop();

    if(stt.x == GX && stt.y == GY) {
      return minCost[stt.y][stt.x][stt.damage];
    }

    rep(i, 4) {
      int nx = dx[i] + stt.x;
      int ny = dy[i] + stt.y;
      if(!(0 <= nx && nx < N && 0 <= ny && ny < N)) continue;
      if(stt.damage + L[ny][nx] >= V) continue;
      if(minCost[ny][nx][stt.damage + L[ny][nx]] <= minCost[stt.y][stt.x][stt.damage] + 1) continue;
      minCost[ny][nx][stt.damage + L[ny][nx]] = minCost[stt.y][stt.x][stt.damage] + 1;
      Q.push(State(nx, ny, stt.damage + L[ny][nx]));
    }
  }

  return -1;
}

int main() {
  // ios_base::sync_with_stdio(false);
  cin >> N >> V >> SX >> SY >> GX >> GY;
  rep(i, N) rep(j, N) cin >> L[i][j];
  cout << solve() << endl;
  return 0;
}
0