結果

問題 No.424 立体迷路
ユーザー nak3nak3
提出日時 2017-03-26 14:19:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 1,333 ms
コンパイル使用メモリ 151,540 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 17:01:01
合計ジャッジ時間 2,334 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define INF 2000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> P;


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

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

	bool rec[h+1][w+1];
	for (int i = 0; i <= h; i++) {
		for (int j = 0; j <= w; j++) {
			rec[i][j] = false;
		}
	}
	// start


	queue<P> q;
	q.push(P(sx,sy));
	while (true) {
		if (q.empty()) {
			cout << "NO" << "\n";
			return 0;
		}
		P tmp = q.front(); q.pop();
		int x = tmp.first;
		int y = tmp.second;
		if (x==gx&&y==gy) {
			cout << "YES" << "\n";
			return 0;
		}

//		cout << "debug " << x << " " << y << "\n";

		if (rec[x][y]) {
			continue;
		}
		if (x-1>=1&&abs((b[x][y-1]-'0')-(b[x-1][y-1]-'0'))<=1) {
			q.push(P(x-1,y));
		}
		if (y-1>=1&&abs((b[x][y-1]-'0')-(b[x][y-1-1]-'0'))<=1) {
			q.push(P(x,y-1));
		}
		if (x+1<=h&&abs((b[x][y-1]-'0')-(b[x+1][y-1]-'0'))<=1) {
			q.push(P(x+1,y));
		}
		if (y+1<=w&&abs((b[x][y-1]-'0')-(b[x][y+1-1]-'0'))<=1) {
			q.push(P(x,y+1));
		}


		if (x-2>=1&&(b[x][y-1]==b[x-2][y-1])&&(b[x-1][y-1]-'0')<=(b[x-2][y-1]-'0')) {
			q.push(P(x-2,y));
		}
		if (y-2>=1&&(b[x][y-1]==b[x][y-2-1])&&(b[x][y-1-1]-'0')<=(b[x][y-2-1]-'0')) {
			q.push(P(x,y-2));
		}
		if (x+2<=h&&(b[x][y-1]==b[x+2][y-1])&&(b[x+1][y-1]-'0')<=(b[x][y-1]-'0')) {
			q.push(P(x+2,y));
		}
		if (y+2<=w&&(b[x][y-1]==b[x][y+2-1])&&(b[x][y+1-1]-'0')<=(b[x][y+2-1]-'0')) {
			q.push(P(x,y+2));
		}
		rec[x][y] = true;
	}
}
0