結果

問題 No.34 砂漠の行商人
ユーザー purple_jwlpurple_jwl
提出日時 2015-03-21 01:37:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 934 ms / 5,000 ms
コード長 1,574 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 148,640 KB
実行使用メモリ 395,156 KB
最終ジャッジ日時 2023-09-10 19:07:17
合計ジャッジ時間 7,103 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,484 KB
testcase_01 AC 3 ms
9,508 KB
testcase_02 AC 14 ms
60,768 KB
testcase_03 AC 12 ms
54,652 KB
testcase_04 AC 52 ms
196,164 KB
testcase_05 AC 50 ms
200,444 KB
testcase_06 AC 33 ms
163,316 KB
testcase_07 AC 60 ms
204,828 KB
testcase_08 AC 69 ms
211,088 KB
testcase_09 AC 345 ms
277,080 KB
testcase_10 AC 178 ms
394,212 KB
testcase_11 AC 615 ms
395,156 KB
testcase_12 AC 46 ms
200,408 KB
testcase_13 AC 934 ms
268,372 KB
testcase_14 AC 717 ms
264,428 KB
testcase_15 AC 52 ms
202,136 KB
testcase_16 AC 88 ms
202,512 KB
testcase_17 AC 58 ms
214,908 KB
testcase_18 AC 20 ms
73,404 KB
testcase_19 AC 223 ms
225,344 KB
testcase_20 AC 365 ms
234,516 KB
testcase_21 AC 58 ms
218,424 KB
testcase_22 AC 73 ms
229,596 KB
testcase_23 AC 74 ms
225,276 KB
testcase_24 AC 496 ms
312,380 KB
testcase_25 AC 67 ms
207,044 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][10000 + 1];

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