結果
問題 | No.424 立体迷路 |
ユーザー |
![]() |
提出日時 | 2016-10-09 21:38:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 12 ms / 2,000 ms |
コード長 | 1,912 bytes |
コンパイル時間 | 584 ms |
コンパイル使用メモリ | 76,520 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 07:11:23 |
合計ジャッジ時間 | 1,264 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
#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;}