結果

問題 No.424 立体迷路
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-03-18 17:03:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 73,912 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 17:06:31
合計ジャッジ時間 1,741 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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] := iの親。iが親だったら 要素数 * -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;
}
0