結果

問題 No.424 立体迷路
ユーザー wunderkammer2wunderkammer2
提出日時 2020-02-29 10:20:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,066 bytes
コンパイル時間 1,168 ms
コンパイル使用メモリ 104,176 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 21:29:41
合計ジャッジ時間 2,115 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<string>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define repl(i,s,e) for(int i=s;i<e;i++)
#define reple(i,s,e) for(int i=s;i<=e;i++)
#define revrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()

int H, W;

class UnionFind
{
	//par[i]:ノードiの親
	//count[i]:iが親の時、iが属するグループのノード数
	vector<int> par;
	vector<int> count;

	int m_groupCount = 0;

public:
	UnionFind(int N)
	{
		par.push_back(0);
		count.push_back(0);

		m_groupCount = N;

		for (int i = 1; i <= N; i++)
		{
			//最初は全てが根であるとして初期化
			par.push_back(i);
			count.push_back(1);
		}
	}

	//データxが属する木の根を再帰で得る:root(x) = {xの木の根}
	int root(int x)
	{
		if (par[x] == x) return x;

		//ついでに親を張り替え
		return par[x] = root(par[x]);
	}

	//xとyの木を併合
	void unite(int x, int y)
	{
		int rx = root(x);
		int ry = root(y);
		if (rx == ry) return;
		//xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
		par[rx] = ry;

		//要素数の変更
		count[ry] += count[rx];
		count[rx] = 0;

		//木の併合によってグループが1つ減る
		m_groupCount--;
	}

	// 2つのデータx, yが属する木が同じならtrueを返す
	bool same(int x, int y)
	{
		return root(x) == root(y);
	}

	//指定したデータxが属するグループの要素数を返す
	int size(int x)
	{
		return count[root(x)];
	}

	//UnionFindのグループの個数を返す
	int groupCount()
	{
		return m_groupCount;
	}
};


int getIndex(int h, int w)
{
	return h * W + w;
}

bool connected(int h1, int w1, int h2, int w2, vector<vector<int>> &maze)
{
	if (
		   h1 < 0 || h1 >= H || w1 < 0 || w1 >= W
		|| h2 < 0 || h2 >= H || w2 < 0 || w2 >= W
		)
		return false;

	if (h1 == h2 && (w1 == w2 - 1 || w1 == w2 + 1) || w1 == w2 && (h1 == h2 - 1 || h1 == h2 + 1))
	{
		return    
			   maze[h1][w1] == maze[h2][w2] - 1
			|| maze[h1][w1] == maze[h2][w2]
			|| maze[h1][w1] == maze[h2][w2] + 1;
	}

	if (h1 == h2 && (w1 == w2 - 2 || w1 == w2 + 2) || w1 == w2 && (h1 == h2 - 2 || h1 == h2 + 2))
	{
		return maze[h1][w1] == maze[h2][w2];
	}

	return false;
}

int main()
{	
	cin >> H >> W;

	int sx, sy, gx, gy;
	cin >> sx >> sy >> gx >> gy;

	--sx;
	--sy;
	--gx;
	--gy;

	vector<vector<int>> maze(H, vector<int>(W, 0));
	rep(i, H)
	{
		string s;
		cin >> s;

		rep(j, W)
		{
			maze[i][j] = s[j] - '0';
		}
	}

	UnionFind uf(H* W);

	rep(i, H)
	{
		rep(j, W)
		{
			int idx1 = getIndex(i, j);

			{
				int h = i - 1;
				int w = j;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i + 1;
				int w = j;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i;
				int w = j - 1;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i;
				int w = j + 1;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i - 2;
				int w = j;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i + 2;
				int w = j;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i;
				int w = j - 2;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}

			{
				int h = i;
				int w = j + 2;

				int idx2 = getIndex(h, w);

				if (connected(i, j, h, w, maze)) uf.unite(idx1, idx2);
			}
		}
	}


	if (uf.same(getIndex(sx, sy), getIndex(gx, gy)))
		cout << "YES" << endl;
	else
		cout << "NO" << endl;


	return 0;
}
0