結果

問題 No.34 砂漠の行商人
ユーザー yuustiyuusti
提出日時 2015-02-15 18:23:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 719 ms / 5,000 ms
コード長 2,041 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 80,996 KB
実行使用メモリ 52,208 KB
最終ジャッジ日時 2023-09-10 18:55:34
合計ジャッジ時間 3,167 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
6,232 KB
testcase_04 AC 19 ms
9,012 KB
testcase_05 AC 20 ms
8,924 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 30 ms
9,244 KB
testcase_08 AC 42 ms
11,544 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 15 ms
8,780 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 17 ms
52,208 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 398 ms
27,648 KB
testcase_20 AC 719 ms
43,956 KB
testcase_21 AC 5 ms
5,280 KB
testcase_22 AC 23 ms
46,516 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 47 ms
15,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <climits>
#include <bitset>
#include <cassert>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

const int N = 101;
int dist[2000][N][N];

struct state{
	int x, y, h, d;
	bool operator < (const state &rhs) const{
		return d > rhs.d;
	}
};

int field[111][111];

int& dist_ref(state &st){
	return dist[st.h][st.x][st.y];
};

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(20);

	int n, v, sx, sy, gx, gy;
	cin >> n >> v >> sx >> sy >> gx >> gy;
	--sx, --sy, --gx, --gy;
	if (v >= 2000){
		cout << abs(gx - sx) + abs(sy - gy) << endl;
		return 0;
	}

	rep(i, n) rep(j, n) cin >> field[i][j];

	state st = { sx, sy, v, 0 };
	priority_queue<state> q;
	rep(i, v + 1) rep(j, n) rep(k, n) dist[i][j][k] = 1e9;
	dist_ref(st) = 1;
	q.push(st);

	while (!q.empty()){
		auto st = q.top();
		q.pop();

		if (dist_ref(st) < st.d) continue;
		if (st.x == gx && st.y == gy){
			cout << st.d << endl;
			return 0;
		}

		rep(i, 4){
			int nx = st.x + dx[i], ny = st.y + dy[i];
			if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
			int nh = st.h - field[ny][nx];
			if (nh <= 0) continue;
			state nxt = { nx, ny, nh, st.d + 1 };
			int &d = dist_ref(nxt);
			if (d <= nxt.d) continue;
			d = nxt.d;
			q.push(nxt);
		}
	}

	cout << -1 << endl;
}
0