AcWing1250. 格子游戏

AcWing1250. 格子游戏

Alice和Bob玩了一个古老的游戏:首先画一个 n×n 的点阵(下图 n=3)。

接着,他们两个轮流在相邻的点之间画上红边和蓝边:

直到围成一个封闭的圈(面积不必为 1)为止,“封圈”的那个人就是赢家。因为棋盘实在是太大了,他们的游戏实在是太长了!

他们甚至在游戏中都不知道谁赢得了游戏。

于是请你写一个程序,帮助他们计算他们是否结束了游戏?

输入格式

输入数据第一行为两个整数 n 和 m。n表示点阵的大小,m 表示一共画了 m 条线。

以后 m 行,每行首先有两个数字 (x,y),代表了画线的起点坐标,接着用空格隔开一个字符,假如字符是 D,则是向下连一条边,如果是 R 就是向右连一条边。

输入数据不会有重复的边且保证正确。

输出格式

输出一行:在第几步的时候结束。

假如 m 步之后也没有结束,则输出一行“draw”。

数据范围

1 ≤ n ≤ 200 1≤n≤200 1n200
1 ≤ m ≤ 24000 1≤m≤24000 1m24000

代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
inline int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
    return x & -x; }


using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 50000;
int f[N];
int n,m;
int Find(int x) {
   
	return x == f[x] ? x : f[x] = Find(f[x]);
}

int get(int x, int y) {
   
	return x * n + y;
}

void merge(int a, int b) {
   
	f[Find(a)] = Find(b);
}

int main() {
   
	cin >> n >> m;

	for (int i = 0; i < N;++i)f[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);
		int b = 0;
		if (d == 'D')b = get(x + 1, y);
		else b = get(x, y + 1);

		if (Find(a) != Find(b))merge(a, b);
		else {
   
			res = i;
			break;
		}
	}

	if (!res)puts("draw");
	else cout << res << endl;
	return 0;
}
全部评论

相关推荐

不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务