You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 200 + 10;
int n, m;
int p[N];
// 二维转一维的办法,坐标从(1,1)开始
int get(int x, int y) {
return (x - 1) * n + y;
}
// 最简并查集
int find(int x) {
if (p[x] != x) p[x] = find(p[x]); // 路径压缩
return p[x];
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n * n; i++) p[i] = i; // 并查集初始化
int res = 0;
for (int i = 1; i <= m; i++) {
int x, y;
char d;
cin >> x >> y >> d;
int a = get(x, y); // 计算a点点号
int b;
if (d == 'D') // 向下走
b = get(x + 1, y);
else // 向右走
b = get(x, y + 1);
// a,b需要两次相遇才是出现了环~
int pa = find(a), pb = find(b);
if (pa == pb) {
res = i; // 记录操作步数
break;
}
// 合并并查集
p[pa] = pb;
}
if (!res) // 没有修改过这个值
puts("draw"); // 平局
else // 输出操作步数
printf("%d\n", res);
return 0;
}