Files
python/TangDou/AcWing_TiGao/T4/1250.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

50 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}