并查集——格子游戏

本文介绍了一款名为格子游戏的两人对弈游戏,并提供了一个程序解决方案,用于判断游戏是否结束及胜者。游戏中,玩家轮流在n×n点阵中绘制红线或蓝线形成封闭圈,最终确定胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 O: 【例4-8】格子游戏

时间限制: 1 Sec  内存限制: 128 MB
提交: 15  解决: 11
[提交][状态][讨论版][命题人:quanxing]

题目描述

 

 

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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int x,y;
}pre[201][201],k1,k2;
int n,m,a,b;
char k;
node find(node n1)
{
    if(pre[n1.x][n1.y].x==n1.x&&pre[n1.x][n1.y].y==n1.y)return n1;
    else return pre[n1.x][n1.y]=find(pre[n1.x][n1.y]);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
          pre[i][j].x=i;
          pre[i][j].y=j;
        }
    for(int i=0;i<m;i++)
    {
        cin>>a>>b>>k;
        if(k=='D')
        {
            k1=find(pre[a][b]);
            k2=find(pre[a+1][b]);
        }
        else if(k=='R')
        {
            k1=find(pre[a][b]);
            k2=find(pre[a][b+1]);
        }
        if(k1.x==k2.x&&k1.y==k2.y)
        {
            cout<<i+1<<endl;
            return 0;
        }
        else pre[k2.x][k2.y]=k1;
    }
    cout<<"draw"<<endl;
    return 0;
}

 

D. Segments Covering time limit per test2 seconds memory limit per test256 megabytes    There is a linear strip divided into m cells, numbered from 1 to m from left to right. You are given n segments. Each segment is defined by four numbers: l , r , p and q — the segment covers cells from l to r inclusively and exists with probability pq (independently). Your task is to calculate the probability that each cell is covered by exactly one segment. AI 翻译    有一个线性长条被分成了 m 个格子,从左到右依次编号为 1 到 m 。 给定 n 个区间。每个区间由四个数定义:l 、r 、p 和 q —— 该区间覆盖从 l 到 r (包含 l 和 r )的格子,且该区间存在的概率为 pq (各区间相互独立)。 你的任务是计算每个格子恰好被一个区间覆盖的概率。    Input The first line contains two integers n and m (1≤n,m≤2⋅105 ). Then n lines follow. The i -th of them contains four integers li , ri , pi and qi (1≤li≤ri≤m ; 1≤pi<qi<998244353 ). AI 翻译    输入 第一行包含两个整数 n 和 m (1≤n,m≤2⋅105 )。 接下来有 n 行。其中第 i 行包含四个整数 li 、ri 、pi 和 qi (1≤li≤ri≤m ;1≤pi<qi<998244353 )。    Output Print a single integer — the probability that each cell is covered by exactly one segment, taken modulo 998244353 . Formally, the probability can be expressed as an irreducible fraction xy . You have to print the value of x⋅y−1mod998244353 , where y−1 is an integer such that y⋅y−1mod998244353=1 . AI 翻译    输出 输出一个整数 —— 每个单元格恰好被一个线段覆盖的概率,对 998244353 取模。 形式上,该概率可以表示为一个既约分数 xy 。你需要输出 x⋅y−1mod998244353 的值,其中 y−1 是一个整数,满足 y⋅y−1mod998244353=1 。 Examples InputCopy 3 3 1 2 1 3 3 3 1 2 1 3 2 3 OutputCopy 610038216 InputCopy 2 3 1 2 1 2 2 3 1 2 OutputCopy 0 InputCopy 8 5 1 3 1 2 1 5 1 6 1 4 4 5 5 5 1 7 4 5 1 2 4 5 2 5 3 3 2 7 1 2 1 3 OutputCopy 94391813    Note In the first example, the probability is equal to 518 . AI 翻译    注意 在第一个示例中,概率等于 518 。解决这个问题,并且给我一个c++代码
最新发布
07-23
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值