转化成编号,如果两个编号并起来之前就已经加入公共节点了,那么说明围城圈了
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 40010;
int n, m;
int p[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int get(int x, int y)
{
return x * n + y;
}
int main()
{
int res = 0;
cin >> n >> m;
for (int i = 1; i <= n * n; i ++ ) p[i] = i;
for (int i = 1; i <= m; i ++ )
{
int x, y;
char c;
cin >> x >> y >> c;
x -- , y -- ;
int a = get(x, y);
int b;
if (c == 'D') b = get(x + 1, y);
else b = get(x, y + 1);
int pa = find(a), pb = find(b);
if (pa == pb)
{
res = i;
break;
}
p[pa] = pb;
}
if (res == 0) cout << "draw" << endl;
else cout << res << endl;
}