HDU--1507 Uncle Tom's Inherited Land* (二分匹配)

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)
 
思路:二分匹配。矩阵存图,白色格子为true,黑色为false,将坐标转化成点,每个点只能和上下左右四个点匹配,然后遍历所有点进行二分匹配。
AC代码:
#include<cstdio>
#include<cstring>
const int N=110;
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool map[N][N],vis[N*N];
int pre[N*N],used[N*N];
int n,m;
bool ok(int x,int y)//判断点是否越界
{
    if(x<0||y<0||x>=n||y>=m)
        return false;
    return true;
}
int find(int k)
{
    int i;
    if(!map[k/m][k%m]) return 0;//判断此点是否为白色
    for(i=0;i<4;i++)
    {
        int nextx=k/m+move[i][0];
        int nexty=k%m+move[i][1];
        int xy=nextx*m+nexty;//将坐标转化成点
        if(ok(nextx,nexty)&&!vis[xy]&&map[nextx][nexty])
        {
            vis[xy]=true;
            if(pre[xy]==-1||find(pre[xy]))
            {
                pre[xy]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int k,i,a,b,sum;
    while(scanf("%d%d",&n,&m),n+m)
    {
        memset(map,true,sizeof(map));//将矩阵初始化为true
        memset(pre,-1,sizeof(pre));//记录与点i匹配的点pre[i]
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&a,&b);
            map[a-1][b-1]=false;
        }
        sum=0;
        for(i=0;i<n*m;i++)
        {
            memset(vis,false,sizeof(vis));
            sum+=find(i);
        }
        memset(used,0,sizeof(used));
        printf("%d\n",sum/2);//匹配次数重复
        for(i=0;i<n*m;i++)
        {
            if(map[i/m][i%m]&&pre[i]!=-1&&!used[i]&&!used[pre[i]])
            {//used记录已输出的点,防止重复输出(因为匹配次数重复)
                printf("(%d,%d)--(%d,%d)\n",i/m+1,i%m+1,pre[i]/m+1,pre[i]%m+1);
                used[i]=used[pre[i]]=1;
            }
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值