hdu1507(二分匹配)

大意是求最多能有多少组白块,相邻的两块为一组,所以这道题目的两个集合可以设定为横纵坐标之和为奇数的集合和横纵坐标之和为偶数的集合,然后建图,一个点只能与之上下左右的点相连,所以只需考虑这四个方向就可以了。

这里写图片描述“`

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int n, m, k;
int d[4][2] = {-1,0,1,0,0,-1,0,1};
void hungarian();
bool dfs(int u);
bool judge(int xx, int yy);
int matching[13030], used[13030], plan[13030];
vector <int> g[13030];
int main()
{
    while(scanf("%d%d", &n, &m), n != 0 && m != 0){
    scanf("%d", &k);
    memset(g, 0, sizeof(g));
    memset(plan, 0,  sizeof(plan));
    for(int i = 0; i < k; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        plan[(x - 1) * m + y] = 1;
    }
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(!plan[(i - 1) * m + j] && (i + j) % 2)
            {
                for(int q = 0; q < 4; q++)
                {
                    int xx = i + d[q][0];
                    int yy = j + d[q][1];
                    if(judge(xx, yy))
                    {
                        g[(i - 1) * m + j].push_back((xx - 1) * m + yy);
                    }

                }
            }
        }
    }
    hungarian();
    }
}
bool judge(int xx, int yy)
{
    if(plan[(xx - 1) * m + yy] == 0 && xx > 0 && xx <= n && yy > 0 && yy <= m)
        return true;
    return false;
}
void hungarian()
{
    int ans = 0;
    memset(matching, -1, sizeof(matching));
    for(int i = 1; i <= n*m; i++)
    {
        memset(used, 0, sizeof(used));
        if(dfs(i))
            ans++;
    }
    printf("%d\n", ans);
    for(int i= 1; i <= n * m; i++)
    {
        int x1, x2, y1, y2;
        if(matching[i] != -1)
        {
            if(!(matching[i] % m))
            {
                x1 = matching[i] / m;
                y1 = m;
            }
            else
            {
                x1 = matching[i] / m + 1;
                y1 = matching[i] % m;
            }
            if(!(i % m))
            {
                x2 = i / m;
                y2 = m;
            }
            else
            {
                x2 = i / m + 1;
                y2 = i % m;
            }
            printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2);
        }
    }
}
bool dfs(int u)
{
    for(int i = 0; i < g[u].size(); i++)
    {
        int t = g[u][i];
        if(!used[t])
        {
            used[t] = 1;
            if(matching[t] == -1 || dfs(matching[t]))
            {
                matching[t] = u;
                return true;
            }
        }
    }
    return false;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Acnidouwo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值