結果

問題 No.1588 Connection
ユーザー kenskens
提出日時 2021-07-08 23:42:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,777 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 206,740 KB
最終ジャッジ日時 2025-01-22 19:37:15
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<int,int>;
constexpr int dx[4] = {0,1,0,-1};
constexpr int dy[4] = {1,0,-1,0};

class Union_Find{
  vector<int> par; vector<int> rank; vector<int> num;
public:
  Union_Find(int n) {
    par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1);
    rep(i,n) par[i] = i;
  }
  int find(int x) {
    if(par[x] == x) return x;
    else  return par[x] = find(par[x]);
  }
  int number(int x) {
    return num[find(x)];
  }
  void unite(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) swap(x, y);
    if (rank[x] == rank[y]) rank[x]++;
    par[y] = x;
    num[x] += num[y];
  } 
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

int main(){
  int n, m;
  cin >> n >> m;
  vector<vector<int>> c(n,vector<int>(n,-1));
  c[0][0] = 1; c[n-1][n-1] = 1;
  queue<P> que;
  que.push(P(1,0));
  que.push(P(0,1));
  while(que.size()) {
    P p = que.front();
    que.pop();
    if(c[p.first][p.second] != -1) continue;
    cout << p.first+1 << " " << p.second+1 << endl;
    fflush(stdout);
    string s;
    cin >> s;
    c[p.first][p.second] = (s == "Black" ? 1 : 0);
    if(c[p.first][p.second] == 1) {
      rep(d,4) {
        int x = p.first+dx[d], y = p.second+dy[d];
        if(x >= 0 and y >= 0 and x < n and y < n and c[x][y] == -1) que.push(P(x,y));
      }
    }
  }
  Union_Find uf(n*n);
  rep(x,n) rep(y,n) {
    rep(d,4) {
      int nx = x+dx[d], ny = y+dy[d];
      if(nx >= 0 and nx < n and ny >= 0 and ny < n and c[x][y] == 1 and c[nx][ny] == 1) uf.unite(x*n+y,nx*n+ny);
    }
  }
  cout << (uf.same(0,n*n-1) ? "Yes" : "No") << endl;
  fflush(stdout);
  return 0;
}
0