分析:二维转一维,我们发现,每次连边,两个结点可以看为在一个集合中,如果封闭了,就说明两个连边的结点已经再一个集合中了,由于是二维的,可以转换为二维
xx=x*n+y
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 200 + 10;
int p[N * N];
int line[N * N];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
int main()
{
int n, m;
cin >> n >> m;
bool flag = false;
int res = -1;
for (int i = 1;i < N * N;i++)
p[i] = i;
for (int i = 1;i <= m;i++)
{
int x, y;char d;
cin >> x >> y >> d;
if (d == 'R')
{
int xx = (y)*n + x, yy = (y + 1) * n + x;
xx = find(xx), yy = find(yy);
if (xx == yy) {
res = i; break;
}
else p[xx] = yy;
}
else
{
int xx = y * n + x, yy = y * n + (x + 1);
xx = find(xx), yy = find(yy);
if (xx == yy)
{
res = i;break;
}
else p[xx] = yy;
}
}
if (res == -1) cout << "draw";
else cout << res << endl;
return 0;
}