NC236756 画圈游戏 (二分图最大匹配)

题目描述
Playf在玩画圈游戏。Playf有一张大小为 n×m 的方格图,其中有一些方格被涂上了星星的图案。Playf可以在方格内画圈,每个圈只能覆盖相邻的两个方格。Playf想要让每一个有星星图案的方格都被他画的圈覆盖,他至少要画多少个圈。
定义两个方格是相邻的,当且仅当它们有一条公共的边。
题目链接:
画圈游戏
思路:
二分图最大匹配,板子题
将整个棋盘分为横坐标与纵坐标的和为奇数和偶数的两种,此时,棋盘就变为了一个二分图。

仅针对和为奇数的点而言,若这个点为 ′ ∗ ′ '*' ,如果它能匹配到邻近的也为 ′ ∗ ′ '*' 的点,那么cnt就加二。

cnt代表被能覆盖两个星星的牌。

而cnt_代表总的星星数。

那么最后的结果就是cnt/2+(cnt_-cnt)
参考 https://blog.youkuaiyun.com/weixin_50616227/article/details/126093113?spm=1001.2014.3001.5501
代码:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
typedef pair<int,int>PII;
vector<PII>ve;
char g[105][105];
bool st[105][105];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
int n,m;
PII match[105][105];
bool find(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int a=x+dx[i];
        int b=y+dy[i];
        if(a<1 || a>n || b<1 || b>m)
        {
            continue;
        }
        if(st[a][b])
        {
            continue;
        }
        if(g[a][b]=='o')
        {
            continue;
        }
        st[a][b]=true;
        if(match[a][b].first == -1 || find(match[a][b].first,match[a][b].second))
        {
            match[a][b]={x,y};
            return true;
        }
    }
    return false;
}
int main()
{
    cin>>n>>m;
    int cnt_=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>g[i][j];
            match[i][j]={-1,-1};
            if(g[i][j]=='*')
            {
                cnt_++;
            }
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if((i+j)%2)
            {
                if(g[i][j]=='*')
                {
                    memset(st,false,sizeof st);
                    if(find(i,j))
                    {
                        cnt+=2;
                    }
                }
            }
        }
    }
    cout<<cnt/2+(cnt_-cnt)<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值