結果

問題 No.20 砂漠のオアシス
ユーザー masamasa
提出日時 2015-01-26 18:15:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 1,662 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 74,068 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 07:23:15
合計ジャッジ時間 1,727 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 23 ms
4,376 KB
testcase_08 AC 9 ms
4,380 KB
testcase_09 AC 17 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;
const int INF = 1e9;

vector< vector<int> > dessert;
vector< vector<int> > hp;
int n;

void show(vector< vector<int> > &vec) {
	int n = vec.size();

	for (int i = 1; i < n - 1; i++) {
		for (int j = 1; j < n - 1; j++) {
			printf("%3d ", vec[i][j]);
		}
		printf("\n");
	}
}


bool walk(int sx, int sy) {
	queue< pair<int, int> > que;
	que.push(make_pair(sx, sy));

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

	int x, y, nx, ny;
	while (!que.empty()) {
		auto now = que.front();
		x = now.first;
		y = now.second;
		que.pop();

		for (int i = 0; i < 4; i++) {
			nx = x + dx[i];
			ny = y + dy[i];

			if (hp[y][x] > dessert[ny][nx] && hp[y][x] - dessert[ny][nx] > hp[ny][nx]) {
				hp[ny][nx] = hp[y][x] - dessert[ny][nx];
				if (nx == n && ny == n) {
					return true;
				}
				que.push(make_pair(nx, ny));
			}
		}
	}
	return false;
}

int main() {
	int v, ox, oy;

	cin >> n >> v >> ox >> oy;
	dessert.assign(n + 2, vector<int>(n + 2, INF));
	hp.assign(n + 2, vector<int>(n + 2, -1));

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> dessert[i][j];
		}
	}
	hp[1][1] = v;
	bool isGoal = walk(1, 1);
	// printf("%s\n", isGoal ? "OK" : "NG");
	// show(hp);
	// cout << endl;

	if (!isGoal) {
		int hp_oasys = hp[oy][ox];
		// printf("%d\n", hp_oasys);
		if (hp_oasys <= 0) {
			isGoal = false;
		} else {
			hp.assign(n + 2, vector<int>(n + 2, -1));
			hp[oy][ox] = 2 * hp_oasys;
			isGoal = walk(ox, oy);
		}
		// show(hp);
	}

	cout << (isGoal ? "YES" : "NO") << endl;
	return 0;
}
0