結果

問題 No.424 立体迷路
ユーザー ei1333333ei1333333
提出日時 2016-09-22 22:30:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,339 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 151,116 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:41:59
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }

  void unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x != y) {
      if(data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }

  int find(int k)
  {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k)
  {
    return (-data[find(k)]);
  }
};

int main()
{
  int H, W;
  int sx, sy, gx, gy;
  string b[50];

  cin >> H >> W;
  cin >> sy >> sx >> gy >> gx;
  for(int i = 0; i < H; i++) {
    cin >> b[i];
  }
  --sx, --sy, --gx, --gy;

  UnionFind tree(H * W);
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      if(j > 0 && abs(b[i][j - 1] - b[i][j]) <= 1) tree.unite(i * W + j, i * W + j - 1);
      if(i > 0 && abs(b[i - 1][j] - b[i][j]) <= 1) tree.unite(i * W + j, i * W + j - W);
      if(j > 1 && b[i][j - 2] == b[i][j] && b[i][j - 2] > b[i][j - 1] && b[i][j] > b[i][j - 1]) tree.unite(i * W + j, i * W + j - 2);
      if(i > 1 && b[i - 2][j] == b[i][j] && b[i - 2][j] > b[i - 1][j] && b[i][j] > b[i - 1][j]) tree.unite(i * W + j, i * W + j - W - W);
    }
  }
  if(tree.find(sy * W + sx) == tree.find(gy * W + gx)) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
0