結果

問題 No.424 立体迷路
ユーザー masamasa
提出日時 2016-09-23 23:58:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,494 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 85,296 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:53:55
合計ジャッジ時間 1,823 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using namespace std;

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

int h, w;

bool inside(int x, int y) {
	return (0 <= x && x < w && 0 <= y && y < h);
}

int main() {
	int sx, sy, gx, gy;
	cin >> h >> w >> sy >> sx >> gy >> gx;
	sx--;
	sy--;
	gx--;
	gy--;

	vector<string> b(h);
	for (int i = 0; i < h; i++) {
		cin >> b[i];
	}

	vector<vector<bool>> went(h, vector<bool>(w, false));
	queue<pair<int, int>> que;

	bool possible = false;
	if (sx == gx && sy == gy) {
		possible = true;
	}

	que.push(make_pair(sx, sy));

	while (!possible && !que.empty()) {
		auto p = que.front();
		que.pop();
		int x = p.first;
		int y = p.second;
		for (int i = 0; i < 4; i++) {
			int nx1 = x + dx[i];
			int ny1 = y + dy[i];
			int nx2 = x + dx[i] * 2;
			int ny2 = y + dy[i] * 2;

			if (inside(nx1, ny1) &&
			    !went[ny1][nx1] &&
			    abs(b[ny1][nx1] - b[y][x]) <= 1)
			{
				went[ny1][nx1] = true;
				que.push(make_pair(nx1, ny1));
				if (nx1 == gx && ny1 == gy) {
					possible = true;
					break;
				}
			}
			if (inside(nx2, ny2) &&
			    !went[ny2][nx2] &&
			    b[ny1][nx1] < b[y][x] &&
			    b[ny2][nx2] == b[y][x])
			{
				went[ny2][nx2] = true;
				que.push(make_pair(nx2, ny2));
				if (nx2 == gx && ny2 == gy) {
					possible = true;
					break;
				}
			}
		}
	}

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