hdu 1507 Uncle Tom's Inherited Land(二分图最大匹配)

28 篇文章 0 订阅

Problem Description

Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle’s request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle’s property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).

这里写图片描述

Input

Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.

Output

For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.

Sample Input

4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
4 3
4
4 2
3 2
2 2
3 1
0 0

Sample Output

4
(1,2)--(1,3)
(2,1)--(3,1)
(2,3)--(3,3)
(2,4)--(3,4)

3
(1,1)--(2,1)
(1,2)--(1,3)
(2,3)--(3,3)

题目大意

叔叔要卖地,‘.’代表土地,‘#’代表池塘,能卖的最小单位是1*2 ,问叔叔最多能卖多少块地,并输出划分的方式。

解题思路

对于二分图问题,首先我们要找到两个独立的集合,然后建立两个集合之间的联系。通过分析这道题目中的信息,我们可以将横纵坐标之和为偶数、横纵坐标为奇数看成两个集合,然后从偶数集合向奇数集合建边,或者从奇数集合向偶数集合建边都可以(代码),然后剩下的就可以对照着裸二分图匹配来了。

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
const int maxn=107;
const int maxm=1e4+7;
int maps[maxn][maxn],ok[maxn][maxn];
int mark[maxn*maxn],vis[maxn*maxn];
int m,n;
int num;  //可售卖的点的个数
struct node
{
    int x;
    int y;
}p[maxm];
void init()
{
    num=0;
    memset(maps,0,sizeof(maps));
    memset(mark,0,sizeof(mark));
    memset(ok,0,sizeof(ok));
}
bool match(int x)
{
    for(int i=1;i<=num;i++)
    {
        if(ok[x][i]&&!vis[i])
        {
            vis[i]=1;
            if(mark[i]==0||match(mark[i]))
            {
                mark[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    IO;
    int k,tx,ty;
    while(cin>>m>>n)
    {
        if(m==0&&n==0) break;
        init();
        cin>>k;
        while(k--)
        {
            cin>>tx>>ty;
            maps[tx][ty]=1;
        }
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
        {
            if(!maps[i][j])
            {
                p[++num].x=i;
                p[num].y=j;
            }
        }
        for(int l=1;l<=num;l++)  //建边
        {
            for(int j=l+1;j<=num;j++)
            {
                if(abs(p[l].x-p[j].x)+abs(p[l].y-p[j].y)==1)
                {
                    if((p[l].x+p[l].y)%2==0)
                        ok[j][l]=1;
                    else ok[l][j]=1;
                }
            }
        }
        int ans=0;
        for(int i=1;i<=num;i++)
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;
        }
        cout<<ans<<endl;
        for(int i=1;i<=num;i++)
            if(mark[i]!=0)
                cout<<"("<<p[i].x<<","<<p[i].y<<")"<<"--("<<p[mark[i]].x<<","<<p[mark[i]].y<<")"<<endl;
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值