結果

問題 No.424 立体迷路
ユーザー yuppe19 😺
提出日時 2019-03-18 17:03:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 73,216 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 07:18:03
合計ジャッジ時間 1,280 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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

#include <array>
#include <iostream>
#include <vector>
using namespace std;
// N E S W
constexpr array<int, 4> dr = {-1, 0, 1, 0},
dc = { 0, 1, 0, -1};
constexpr int dr_size = dr.size();
class UnionFind {
vector<int> par; // par[i] := ii * -1
public:
explicit UnionFind(int n) : par(n, -1) {}
int root(int a); // a
int size(int a); // a
void unite(int a, int b); // a b
};
int UnionFind::root(int a) {
if(par[a] < 0) { return a; }
return par[a] = root(par[a]);
}
int UnionFind::size(int a) {
return -par[root(a)];
}
void UnionFind::unite(int a, int b) {
a = root(a);
b = root(b);
if(a == b) { return; }
if(size(a) < size(b)) { swap(a, b); }
par[a] += par[b];
par[b] = a;
}
int R, C;
void debug(const vector<vector<int>> &G) {
for(int i=0; i<R; ++i) {
for(int j=0; j<C; ++j) {
if(j) { fprintf(stderr, " "); }
fprintf(stderr, "%d", G[i][j]);
}
fprintf(stderr, "\n");
}
}
inline int calc(int r, int c) {
return r * C + c;
}
int main(void) {
scanf("%d%d", &R, &C);
int sr, sc, gr, gc; scanf("%d%d%d%d", &sr, &sc, &gr, &gc);
--sr, --sc, --gr, --gc;
vector<vector<int>> G(R, vector<int>(C, 0));
for(int r=0; r<R; ++r) {
char s[64]; scanf("%s\n", s);
for(int c=0; c<C; ++c) {
G[r][c] = s[c] - '0';
}
}
// debug(G);
UnionFind uf(R*C);
for(int r=0; r<R; ++r) {
for(int c=0; c<C; ++c) {
for(int i=0; i<dr_size; ++i) {
int nr = r + dr[i],
nc = c + dc[i];
if(!(0 <= nr && nr < R && 0 <= nc && nc < C)) { continue; }
if(abs(G[r][c] - G[nr][nc]) <= 1) {
uf.unite(calc(r, c), calc(nr, nc));
}
int nr2 = r + dr[i] * 2,
nc2 = c + dc[i] * 2;
if(!(0 <= nr2 && nr2 < R && 0 <= nc2 && nc2 < C)) { continue; }
if(G[r][c] == G[nr2][nc2] && G[nr][nc] < G[r][c]) {
uf.unite(calc(r, c), calc(nr2, nc2));
}
}
}
}
bool yes = uf.root(calc(sr, sc)) == uf.root(calc(gr, gc));
puts(yes ? "YES" : "NO");
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0