結果

問題 No.34 砂漠の行商人
ユーザー Yang33Yang33
提出日時 2017-05-29 07:41:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 753 ms / 5,000 ms
コード長 1,667 bytes
コンパイル時間 1,369 ms
コンパイル使用メモリ 154,544 KB
実行使用メモリ 395,756 KB
最終ジャッジ日時 2023-09-10 19:28:01
合計ジャッジ時間 9,600 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
394,416 KB
testcase_01 AC 172 ms
394,348 KB
testcase_02 AC 175 ms
394,588 KB
testcase_03 AC 173 ms
394,472 KB
testcase_04 AC 182 ms
394,640 KB
testcase_05 AC 181 ms
394,624 KB
testcase_06 AC 176 ms
394,656 KB
testcase_07 AC 185 ms
394,468 KB
testcase_08 AC 190 ms
394,604 KB
testcase_09 AC 351 ms
395,460 KB
testcase_10 AC 186 ms
394,704 KB
testcase_11 AC 550 ms
395,548 KB
testcase_12 AC 132 ms
394,556 KB
testcase_13 AC 753 ms
395,712 KB
testcase_14 AC 587 ms
395,756 KB
testcase_15 AC 130 ms
394,472 KB
testcase_16 AC 153 ms
394,788 KB
testcase_17 AC 129 ms
394,464 KB
testcase_18 AC 130 ms
394,544 KB
testcase_19 AC 254 ms
395,544 KB
testcase_20 AC 352 ms
395,692 KB
testcase_21 AC 128 ms
394,376 KB
testcase_22 AC 131 ms
394,536 KB
testcase_23 AC 128 ms
394,484 KB
testcase_24 AC 435 ms
395,640 KB
testcase_25 AC 142 ms
394,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define FOR(i, s, e) for (ll(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (ll(i) = (s); (i) > (e); (i)--)
#define debug(x) cout << #x << ": " << x << endl
#define mp make_pair
#define pb push_back
const ll MOD = 1000000007;
const int INF = 1e8;
const ll LINF = 1e16;
const double PI = acos(-1.0);
int dx[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  xtimex  Problem: yukicoder034  / Link: http://yukicoder.me/problems/no/34  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

ll N, V;
int sx, sy, tx, ty;
int masu[100][100];
int dist[100][100][10010];
ll ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> N >> V;
	cin >> sx >> sy >> tx >> ty;
	sx--; sy--; tx--; ty--;
	FOR(i, 0, N) {
		FOR(j, 0, N)cin >> masu[i][j];
	}
	//input
	FOR(i, 0, 100)FOR(j, 0, 100)FOR(k,0,10010)dist[i][j][k] = INF;
	
	using tp = tuple<int, int, int>;
	queue<tp>q;
	q.push(tp(sy, sx, V));
	dist[sy][sx][V] = 0;
	ans = -1;
	while (!q.empty()) {
		//debug(q.size());
		int y, x, v;
		tie(y, x, v) = q.front(); q.pop();
		if (y == ty&&x == tx) {
			cout << dist[ty][tx][v] << endl;
			return 0;
		}
		FOR(i, 0, 4) {
			int ny = y + dy[i];
			int nx = x + dx[i];
			if (0 <= ny&&ny < N && 0 <= nx&&nx < N) {
				int nv = v - masu[ny][nx];
				if (nv >  0 && dist[ny][nx][nv] == INF) {
					dist[ny][nx][nv] = dist[y][x][v] + 1;
					q.push(tp(ny, nx, nv));
				}
			}
		}
	}


	cout << ans << endl;

	return 0;
}
0