結果

問題 No.424 立体迷路
ユーザー olpheolphe
提出日時 2016-09-22 23:07:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 46,144 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:50:20
合計ジャッジ時間 1,382 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include "stdio.h"
#include "queue"
#include "utility"

using namespace std;
typedef pair<int, int>P;
P now;
P goal;
P box;
int H, W;
char hei[51][51];
int height[51][51];
int dx[8] = { 1,0,-1,0,2,0,-2,0 };
int dy[8] = { 0,-1,0,1,0,-2,0,2 };
queue <P> Q;
int dis[51][51];

int abs(int a) {
	if (a > 0)return a;
	return a*-1;
}

int main() {
	scanf("%d %d", &H, &W);
	scanf("%d %d %d %d", &now.first, &now.second, &goal.first, &goal.second);
	for (int i = 1; i <= H; i++) {
		scanf("%s", hei[i]);
	}
	for (int i = 1; i <= H; i++) {
		for (int j = W; j >= 1; j--) {
			hei[i][j] = hei[i][j - 1];
			height[i][j] = hei[i][j] - '0';
			//printf("%d", height[i][j]);
		}
	}
	Q.push(now);
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			dis[i][j] = 10000;
		}
	}
	dis[now.first][now.second] = 0;
	while (!Q.empty()) {
		now = Q.front();
	//	printf("%d %dを調べる\n", now.first, now.second);
		for (int i = 0; i < 8; i++) {
			if (now.first + dy[i] >= 1 && now.first + dy[i] <= H&&now.second + dx[i] >= 1 && now.second + dx[i] <= W) {
				if (i < 4) {
					if (abs(height[now.first][now.second] - height[now.first + dy[i]][now.second + dx[i]])<2) {
						if (dis[now.first][now.second] + 1 < dis[now.first + dy[i]][now.second + dx[i]]) {
							dis[now.first + dy[i]][now.second + dx[i]] = dis[now.first][now.second] + 1;
							box.first = now.first + dy[i];
							box.second = now.second + dx[i];
							Q.push(box);
						}
					}
				}
				else {
					if (height[now.first][now.second] == height[now.first + dy[i]][now.second + dx[i]]) {
						if (height[now.first][now.second] > height[now.first + dy[i] / 2][now.second + dx[i] / 2]) {
							if (dis[now.first][now.second] + 1 < dis[now.first + dy[i]][now.second + dx[i]]) {
								dis[now.first + dy[i]][now.second + dx[i]] = dis[now.first][now.second] + 1;
								box.first = now.first + dy[i];
								box.second = now.second + dx[i];
								Q.push(box);
							}
						}
					}
				}
			}
		}
		Q.pop();
	}
	if (dis[goal.first][goal.second] != 10000)printf("YES\n");
	else printf("NO\n");
	return 0;
}
0