当前位置:网站首页>动规(16)-并查集基础题——格子游戏
动规(16)-并查集基础题——格子游戏
2022-08-04 12:13:00 【H_Cisco】
【问题描述】
Alice和Bob玩了一个古老的游戏:首先画一个n * n的点阵(下图n = 3) 接着,他们两个轮流在相邻的点之间画上红边和蓝边:

直到围成一个封闭的圈(面积不必为1)为止,“封圈”的那个人就是赢家。因为棋盘实在是太大了(n<= 200),他们的游戏实在是太长了!他们甚至在游戏中都不知道谁赢得了游戏。于是请你写一个程序,帮助他们计算他们是否结束了游戏?
【输入格式】
输入数据第一行为两个整数n和m。m表示一共画了m条线。以后m行,每行首先有两个数字(x,y),代表了画线的起点坐标,接着用空格隔开一个字符,假如字符是"D ",则是向下连一条边,如果是"R"就是向右连一条边。输入数据不会有重复的边且保证正确。
【输出格式】
输出一行:在第几步的时候结束。假如m步之后也没有结束,则输出一行“draw”。
【输入样例】
3 5
1 1 D
1 1 R
1 2 D
2 1 R
2 2 D
【输出样例】
4
#include <cstdio>
#include <iostream>
using namespace std;
int fa[40001], n, m, x, y, r1, r2, xx, yy;
char ch;
int find(int u)
{
return fa[u] == u ? u : fa[u] = find(fa[u]);
}
int read()
{
int x = 0;
char ch = getchar();
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - 48, ch = getchar();
return x;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n * n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++)
{
x = read();
y = read();
cin >> ch;
xx = n * (x - 1) + y;
if (ch == 'D')
yy = xx + n;
else
yy = xx + 1;
r1 = find(xx);
r2 = find(yy);
if (r1 != r2)
fa[r1] = r2;
else
{
cout << i;
return 0;
}
}
cout << "draw" << endl;
return 0;
}
边栏推荐
猜你喜欢

Leetcode brush - structure binary tree (105. Once upon a time sequence and the sequence structure binary tree traversal sequence, 106. From the sequence with the sequence structure binary tree travers

推荐一款优秀的通用管理后台

UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!

COVID-CT新冠肺炎检测(DenseNet网络)

LeetCode Daily Question (858. Mirror Reflection)

ESP8266-Arduino编程实例-MQ3酒精传感器驱动

【Qt】解决 “由于找不到Qt5Cored.dll,无法继续执行代码”(亲测有效)

考研概率论与数理统计(知识点梳理)

BOSS直聘回应女大学生连遭两次性骚扰:高度重视求职者安全 可通过App等举报

记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
随机推荐
ECCV 2022 | 通往数据高效的Transformer目标检测器
博云入选 Gartner 中国 DevOps 代表厂商
COVID-CT新冠肺炎检测(DenseNet网络)
隐私计算与数据流通:关系、作用及功能
MySQL - Explain explanation
Neck modules of the yolo series
【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
【目标检测】------yolo:xml和txt文件相互转化
常用代码模板1——基础语法
LeetCode Daily Question (858. Mirror Reflection)
Based on the BiLSTM regression forecast method
表的完整性约束;非外键约束
Practical sharing of distributed link tracking Jaeger + microservice Pig on Rainbond
七夕还没选好礼物,快送这套美妆秘籍,保准没错~~
WPF---Grid布局讲解
数据库对象
What is DevOps?Enough to read this one!
【无标题】
Flutter 使用 json_serializable 解析 JSON 支持泛型
BOSS直聘回应女大学生连遭两次性骚扰:高度重视求职者安全 可通过App等举报