結果
| 問題 |
No.424 立体迷路
|
| コンテスト | |
| ユーザー |
はむこ
|
| 提出日時 | 2016-09-19 08:00:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,000 bytes |
| コンパイル時間 | 2,350 ms |
| コンパイル使用メモリ | 169,732 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-05 06:57:43 |
| 合計ジャッジ時間 | 2,844 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
using ll = long long; using vll = vector<ll>; using vvll = vector<vll>;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
// x, yをマージ
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
// x, yが同じ集合なら1
bool find(int x, int y) {
return root(x) == root(y);
}
// xの根を探す。同じ集合なら同じ根が帰る
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
};
#define get(i, j) (b[i+buff/2][j+buff/2])
#define encode(i, j) (i*w+j)
int main(void) {
cin.tie(0); ios::sync_with_stdio(false);
ll h, w; cin >> h >> w;
ll sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
sx--; sy--; gx--; gy--;
const int buff = 10;
vvll b(h+buff, vll(w+buff, -5));
rep(i, h) {
string s; cin >> s;
rep(j, s.length()) {
get(i, j) = s[j] - '0';
}
}
vll di = {1, -1, 0, 0};
vll dj = {0, 0, 1, -1};
UnionFind uf(h*w);
rep(i, h) {
rep(j, w) {
rep(d, 4) {
ll nexti = i + di[d];
ll nextj = j + dj[d];
if (abs(get(i, j) - get(nexti, nextj)) <= 1)
uf.unite(encode(i, j), encode(nexti, nextj));
}
rep(d, 4) {
ll nexti = i + di[d] * 2;
ll nextj = j + dj[d] * 2;
if (get(i, j) == get(nexti, nextj) && get(i, j) > get(i + di[d], j + dj[d]))
uf.unite(encode(i, j), encode(nexti, nextj));
}
}
}
if (uf.find(encode(sx, sy), encode(gx, gy))) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
はむこ