首页 > 其他 > 详细

并查集

时间:2021-05-15 19:33:07      阅读:30      评论:0      收藏:0      [点我收藏+]

普通并查集

格子游戏

题目描述
技术分享图片

参考题解

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 4e4+5;

int n, m;

int p[N];

int find(int x)
{
    if(x != p[x])   p[x] = find(p[x]);
    return p[x];
}

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

int main()
{
    cin >> n >> m;
    for(int i = 0; i < N; ++ i) p[i] = i;
    int ans = 0;
    for(int step = 1; step <= m; ++ step)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x -= 1, y -= 1; 
        char op[2];
        scanf("%s", op);
        if(ans > 0) continue;
        int a, b;
        a = get(x, y);
        if(op[0] == ‘D‘)
        {
            b = get(x+1, y);
        }
        else
        {
            b = get(x, y+1);
        }
        a = find(a), b = find(b);
        if(a == b)
        {
            ans = step;
        }
        p[a] = b;
    }
    if(ans > 0)
    {
        printf("%d\n", ans);
    }
    else
    {
        cout << "draw" << endl;
    }
    return 0;
}

并查集

原文:https://www.cnblogs.com/chaosliang/p/14771658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!