結果

問題 No.424 立体迷路
ユーザー nenuonnenuon
提出日時 2017-08-04 14:03:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,986 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 88,704 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 17:02:17
合計ジャッジ時間 2,050 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

// タグにunion-findとあったのでunion-findでやってみる
// (y,x)について2個以内のブロックに行けたら同じグループ
// スタートとゴールが同じグループならいける

// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n要素で初期化
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//木の根を求める
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) par[x] = y;
    else{par[y] = x;if(rnk[x] == rnk[y]) rnk[x]++;}
    return;
}
bool isSame(int x, int y){return find(x) == find(y);}

int Z(int y, int x) {
  return 100 * y + x;
}

int main()
{
  int h, w;
  cin >> h >> w;
  init(10000);
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;sy--;gx--;gy--;
  vector<string> vs(h);
  FOR(i,0,h) cin >> vs[i];
  int vy[8] = {-1, 0, 1, 0, -2, 0, 2, 0};
  int vx[8] = {0, 1, 0, -1, 0, 2, 0, -2};
  FOR(y,0,h) {
    FOR(x,0,w) {
      FOR(i,0,8) {
        int nx = x + vx[i];
        int ny = y + vy[i];
        if(nx<0||nx>=w||ny<0||ny>=h) continue;
        // 一個横
        if(i<4) {
          if(abs((vs[y][x]-'0')-(vs[ny][nx]-'0')) <= 1) unite(Z(y,x),Z(ny,nx));
        } else {
          if((vs[y][x] == vs[ny][nx]) && ((vs[y][x]-'0') > (vs[y+vy[i-4]][x+vx[i-4]]-'0'))) unite(Z(y,x),Z(ny,nx));
        }
      }
    }
  }
  cout << (isSame(Z(gy,gx),Z(sy,sx)) ? "YES" : "NO") << endl;
  return 0;
}
0