結果

問題 No.424 立体迷路
コンテスト
ユーザー srup٩(๑`н´๑)۶
提出日時 2016-09-23 11:45:17
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,407 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 554 ms
コンパイル使用メモリ 97,360 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-26 14:00:50
合計ジャッジ時間 1,273 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(n);i++)

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int h, w, sx, sy, gx, gy;
string b[51];
bool used[51][51];
int main(void){
	scanf("%d %d %d %d %d %d", &h, &w, &sy, &sx, &gy, &gx);
	sx--; sy--; gx--; gy--;
	rep(i, h) cin >> b[i];
	rep(i, 51)rep(j, 51){
		used[i][j] =  false;
	}

	queue<pair<int, int> > q;
	q.push(make_pair(sy, sx));
	used[sy][sx] = true;
	while(!q.empty()){
		auto n = q.front(); q.pop();
		if(n.fi == gy && n.se == gx) break;
		int hh = b[n.fi][n.se] - '0';

		rep(i, 4){
			int ny = n.fi + dy[i], nx = n.se + dx[i];
			if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
			if(used[ny][nx]) continue;

			int nh = b[ny][nx] - '0';
			if(abs(nh - hh) <= 1){
				used[ny][nx] = true;
				q.push(make_pair(ny, nx));
			}
		}
		rep(i, 4){
			int ny = n.fi + 2 * dy[i], nx = n.se + 2 * dx[i];
			if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
			if(used[ny][nx]) continue;
			int nh = b[ny][nx] - '0';
			int ah = b[(ny + n.fi) / 2][(nx + n.se) / 2] - '0';
			if(hh - ah > 0 && nh == hh){
				used[ny][nx] = true;
				q.push(make_pair(ny, nx));
			}
		}
	}

	if(used[gy][gx]) printf("YES\n");
	else printf("NO\n");
	return 0;
}
0