結果

問題 No.424 立体迷路
ユーザー ei1333333ei1333333
提出日時 2016-09-22 22:27:42
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,340 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 162,264 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 18:46:16
合計ジャッジ時間 2,233 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 WA -
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,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 >> sx >> sy >> gx >> gy;
  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