結果

問題 No.424 立体迷路
ユーザー めうめう🎒めうめう🎒
提出日時 2017-02-01 14:00:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,220 bytes
コンパイル時間 856 ms
コンパイル使用メモリ 79,648 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 17:00:16
合計ジャッジ時間 1,884 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

#define ll long long
#define INF (1 << 30)
#define INFLL (1LL << 60)

#define FOR(i,a,b) for(ll i = (a);i<(b);i++)
#define REP(i,a) FOR(i,0,(a))
#define MP make_pair

int h, w;
int sx, sy, gx, gy;
int hyo[51][51] = {};

int DIRX[4] = {1, -1, 0, 0};
int DIRY[4] = {0, 0, 1, -1};
int DIRX2[4] = {2, -2, 0, 0};
int DIRY2[4] = {0, 0, 2, -2};

bool check(int nowx, int nowy){
	if(nowx < 0 || nowy < 0 || nowy >= h || nowx >= w) return true;
	return false;
}

bool solve(){
	queue<pair<int, int> > que;
	que.push(MP(sx, sy));
	bool used[51][51] = {};
	while(que.size()){
		pair<int, int> data = que.front();que.pop();
		int nowx = data.first;
		int nowy = data.second;


		if(used[nowx][nowy]) continue;
		used[nowx][nowy] = true;
		// cout << nowx << "," << nowy << endl;

		if(nowx == gx && nowy == gy){
			return true;
		}

		for(int i = 0;i < 4;i++){
			int tox = nowx + DIRX[i], toy = nowy + DIRY[i];
			if(check(tox, toy)) continue;
			if(abs(hyo[nowx][nowy] - hyo[tox][toy]) > 1) continue;
			// if(nowx == sx && nowy == sy){
			// 	cout << tox << "," << toy << "-" << hyo[nowx][nowy] << endl;
			// }
			que.push(MP(tox, toy));
		}

		for(int i = 0;i < 4;i++){
			int tox = nowx + DIRX2[i], toy = nowy + DIRY2[i];
			int midx = nowx + DIRX[i], midy = nowy + DIRY[i];
			if(check(tox, toy)) continue;
			if(hyo[nowx][nowy] != hyo[tox][toy]) continue;
			if(hyo[midx][midy] > hyo[tox][toy]) continue;
			que.push(MP(tox, toy));
		}
	}

	return false;
}

int main() {
	cin >> h >> w;
	cin >> sy >> sx >> gy >> gx;
	sx--;gx--;sy--;gy--;
	string str;
	for(int i = 0;i < h;i++){
		cin >> str;
		for(int j = 0;j < str.size();j++){
			hyo[j][i] = str[j] - '0';
		}
	}
	// cout << sx << "--" << sy << endl;
	// cout << hyo[1][3] << endl;

	// cout << "--------------" << endl;
	// for(int i = 0;i < h;i++){
	// 	for(int j = 0;j < w;j++){
	// 		cout << hyo[j][i];
	// 	}
	// 	cout << endl;
	// }

	bool ans = solve();
	if(ans) cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}
0