結果

問題 No.424 立体迷路
ユーザー kurenai3110
提出日時 2016-09-22 23:22:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,396 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 69,740 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-17 15:08:35
合計ジャッジ時間 1,531 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 19 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

typedef pair<int, int> P;

int MAZE[51][51];
int w, h;
int sx, sy, gx, gy;
int d[51][51];

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

int bfs() {
	queue<P> que;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) d[i][j] = INT32_MAX;
	}
	que.push(P(sx, sy));
	d[sx][sy] = 0;

	while (que.size()) {
		P p = que.front(); que.pop();
		if (p.first == gx && p.second == gy) break;

		for (int i = 0; i < 4; i++) {
			int nx = p.first + dx[i], ny = p.second + dy[i];

			if (0 <= nx && nx < h && 0 <= ny && ny < w && abs(MAZE[nx][ny] - MAZE[p.first][p.second]) <= 1 && d[nx][ny] == INT32_MAX) {
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
		for (int i = 0; i < 4; i++) {
			int nx = p.first + 2*dx[i], ny = p.second + 2*dy[i];

			if (0 <= nx && nx < h && 0 <= ny && ny < w && (MAZE[nx][ny] - MAZE[p.first][p.second]) == 0 && d[nx][ny] == INT32_MAX) {
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}
	return d[gx][gy];
}


int main()
{

	cin >> h >> w >> sx >> sy >> gx >> gy;
	sx--; sy--; gx--; gy--;

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			char c;
			cin >> c;
			MAZE[i][j] = c-'0';
		}
	}

	if (bfs() != INT32_MAX)cout << "YES" << endl;
	else cout << "NO" << endl;

    return 0;
}
0