結果

問題 No.424 立体迷路
ユーザー tubo28tubo28
提出日時 2016-09-22 22:58:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,339 bytes
コンパイル時間 1,333 ms
コンパイル使用メモリ 117,484 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-05 07:05:31
合計ジャッジ時間 2,111 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
using ll = long long;
int h, w;
char g[55][55];
int si, sj, gi, gj;
struct UnionFind {
vector<int> par;
int cnt;
UnionFind(int size_) : par(size_, -1), cnt(size_) {}
void unite(int x, int y) {
if ((x = find(x)) != (y = find(y))) {
if (par[y] < par[x]) swap(x, y);
par[x] += par[y]; par[y] = x; cnt--;
}
}
bool same(int x, int y) { return find(x) == find(y); }
int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
int size(int x) { return -par[find(x)]; }
int size() { return cnt; }
};
bool check(int i, int j, int ni, int nj) {
int a = g[i][j] - '0';
int b = g[ni][nj] - '0';
return abs(a - b) <= 1;
}
bool check2(int i, int j, int ni, int nj) {
int a = g[i][j] - '0';
int b = g[ni][nj] - '0';
int c = g[(i + ni) / 2][(j + nj) / 2] - '0';
return a == b && a > c;
}
int enc(int i, int j) {
return i*w + j;
}
int main() {
while (cin >> h >> w) {
UnionFind uf(h*w);
cin >> si >> sj >> gi >> gj;
--si; --sj; --gi; --gj;
for (int i = 0; i < h; i++) cin >> g[i];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i >= 1) if (check(i, j, i - 1, j)) uf.unite(enc(i, j), enc(i - 1, j));
if (j >= 1) if (check(i, j, i, j - 1)) uf.unite(enc(i, j), enc(i, j - 1));
if (i >= 2) if (check2(i, j, i - 2, j)) uf.unite(enc(i, j), enc(i - 2, j));
if (j >= 2) if (check2(i, j, i, j - 2)) uf.unite(enc(i, j), enc(i, j - 2));
}
}
puts(uf.same(enc(si, sj), enc(gi, gj)) ? "YES" : "NO");
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0