Hdu 1507 Uncle Tom's Inherited Land*【二分匹配】

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3473    Accepted Submission(s): 1468
Special Judge


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)
 

Source

题目大意:


老套路,给你一个N*M的图,其中有一些点不能放置木块,一个木块是1*2大小的,可以横着放可以竖着放,问你剩下的格子中,最多能够放多少个木块.

然后让你输出任意一个可行解。


思路:


建图跑二分匹配即可。


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int fx[4]= {0,0,1,-1};
int fy[4]= {1,-1,0,0};
int num[150][150];
int match[150*150];
int vis[150*150];
vector<int >mp[150*150];
int n,m;
void Getmap()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(num[i][j]!=-1)num[i][j]=(i-1)*m+j;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            for(int k=0; k<4; k++)
            {
                int xx=i+fx[k];
                int yy=j+fy[k];
                if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
                {
                    mp[num[i][j]].push_back(num[xx][yy]);
                }
            }
        }
    }
}
int find(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                //match[u]=v;
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void Match()
{
    int output=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n*m;i++)
    {
        memset(vis,0,sizeof(vis));
        if(find(i))output++;
    }
    printf("%d\n",output/2);
    for(int i=1;i<=n*m;i++)
    {
        if(match[i]==-1)continue;
        int x=i/m;if(i%m!=0)x++;
        int y=i%m;
        if(y==0)y=m;
        int xx=match[i]/m;if(match[i]%m!=0)xx++;
        int yy=match[i]%m;
        if(yy==0)yy=m;
        if(num[x][y]!=-1&&num[xx][yy]!=-1)
        {
            num[x][y]=-1;num[xx][yy]=-1;
            printf("(%d,%d)--(%d,%d)\n",x,y,xx,yy);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0||m==0)break;
        int z;
        scanf("%d",&z);
        memset(num,0,sizeof(num));
        for(int i=1;i<=n*m;i++)mp[i].clear();
        for(int i=0; i<z; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            num[x][y]=-1;
        }
        Getmap();
        Match();
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值