結果

問題 No.424 立体迷路
ユーザー alpha_virginisalpha_virginis
提出日時 2016-10-09 21:38:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,912 bytes
コンパイル時間 545 ms
コンパイル使用メモリ 72,432 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:57:22
合計ジャッジ時間 1,681 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>

#include <cstdint>
#include <random>
template<uint32_t size>
struct DisjointSet {
  DisjointSet() {
    for(int i = 0; i < (int)size; ++i) parent[i] = i;
  }
  int parent[size];
};

template<uint32_t size>
int find(DisjointSet<size>& ds, int x) {
  if( ds.parent[x] == x ) return x;
  return ds.parent[x] = find(ds, ds.parent[x]);
}
template<uint32_t size>
void unite(DisjointSet<size>& ds, int x, int y) {
  x = find(ds, x);
  y = find(ds, y);
  if( x == y ) return;
  std::random_device rd;
  if( rd() & 1 ) std::swap(x, y);
  ds.parent[x] = y;
}
template<uint32_t size>
bool same(DisjointSet<size>& ds, int x, int y) {
  return find(ds, x) == find(ds, y);
}

int coord(int r, int c) {
  return r * 64 + c;
}

int main() {
  DisjointSet<64*64> ds;
  int h, w;
  scanf("%d %d", &h, &w);
  int sx, sy, gx, gy;
  scanf("%d %d %d %d", &sx, &sy, &gx, &gy);
  sx -= 1;
  sy -= 1;
  gx -= 1;
  gy -= 1;
  int field[64][64];
  for(int r = 0; r < h; ++r) {
    char buf[64];
    scanf("%s", buf);
    for(int c = 0; c < w; ++c) {
      field[r][c] = buf[c] - '0';
    }
  }
  int dr[4] = {1, 0, -1, 0};
  int dc[4] = {0, 1, 0, -1};
  for(int r = 0; r < h; ++r) {
    for(int c = 0; c < w; ++c) {
      for(int k = 0; k < 4; ++k) {
        int nr = r + dr[k];
        int nc = c + dc[k];
        if( not ( 0 <= nr and nr < h and 0 <= nc and nc < w ) ) continue;
        if( std::abs(field[r][c] - field[nr][nc]) <= 1 ) {
          unite(ds, coord(r, c), coord(nr, nc));
        }
        if( not ( field[r][c] > field[nr][nc] ) ) continue;
        int nr2 = nr + dr[k];
        int nc2 = nc + dc[k];
        if( not ( 0 <= nr2 and nr2 < h and 0 <= nc2 and nc2 < w ) ) continue;
        if( not ( field[r][c] == field[nr2][nc2] ) ) continue;
        unite(ds, coord(r, c), coord(nr2, nc2));        
      }
    }
  }
  
  puts( same(ds, coord(sx, sy), coord(gx, gy)) ? "YES" : "NO" );
  
  return 0;
}
0