結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
purple_jwl
|
| 提出日時 | 2015-03-21 01:37:38 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,113 ms / 5,000 ms |
| コード長 | 1,574 bytes |
| コンパイル時間 | 1,346 ms |
| コンパイル使用メモリ | 162,836 KB |
| 実行使用メモリ | 395,136 KB |
| 最終ジャッジ日時 | 2024-06-28 10:16:08 |
| 合計ジャッジ時間 | 7,670 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#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;
}
purple_jwl