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): 2170    Accepted Submission(s): 897
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)
 



题意:问未标记为水潭的点最多可以放多少1*2的方块。

把棋盘的每个 点横坐标和纵坐标相加,奇数点放二分图左侧,偶数点放右侧。然后历遍这个棋盘,把可以摆1*2的两个点进行匹配。然后最大匹配就是答案了。



#include <stdio.h>
#include <string.h> 

int dir[4][2]={1,0,0,1,-1,0,0,-1};
int mp[110][110];
int id[210*210];//地图点 映射 去离散化后的编号

int jibiao[150];//编号 映射 地图的点
int oubiao[150];
#define N 150
int visit[N];
int mark[N];
int match[N][N];
int n,m,k;
int dfs(int x)
{
    int i;
    for(i=1;i<=m;i++)//对左边的节点x与右边的节点进行逐一检查
    {
        if(!visit[i]&&match[x][i])
        {
            visit[i]=1;//标记检查过的点
            if(mark[i]==-1||dfs(mark[i])) 
            {//|| 前面过了 后面不运行;; 
                mark[i]=x;//修改匹配关系   
                return 1;
            }
        }
    }
    return 0;
}

int hungary ()
{
    memset(mark,-1,sizeof(mark));
	int max=0,j;
    for(j=1;j<=n;j++)//对做部分顶点逐个进行遍历
	{
		memset(visit,0,sizeof(visit));
		if(dfs(j))
			max++;
	}
	return max;
}
 
int main()
{ 
	int num,x,y;
	int nn,mm;
	while(scanf("%d%d",&nn,&mm),nn&&mm)//注意 mark[mm]=nn  mark[j] = i
	{
		scanf("%d",&num);
		memset(mp,0,sizeof mp);
		for(int i=0;i<num;i++)
		{
			scanf("%d%d",&x,&y);
			mp[x][y]=1;//水塘
		}
		int ji=1;
		int ou=1;
		memset(match,0,sizeof(match));
		for(int i=1;i<=nn;i++)
		{
			for(int j=1;j<=mm;j++)
			{
				if(mp[i][j]==0&&((i+j)&1))
				{
					jibiao[ji]=(i-1)*mm+j-1;
					id[(i-1)*mm+j-1]=ji++;
				}
				else if(mp[i][j]==0)
				{
					oubiao[ou]=(i-1)*mm+j-1;
					id[(i-1)*mm+j-1]=ou++;
				}
			}
		}
		n=ji-1;//去离散化后  二分图中的n和m
		m=ou-1;
		for(int i=1;i<=nn;i++)
		{
			for(int j=1;j<=mm;j++)
			{
				if((!mp[i][j])&&(i+j)%2==1)
				for(int k=0;k<4;k++)
				{
					int xx,yy;
					xx=i+dir[k][0];
					yy=j+dir[k][1];
					if(xx<1||xx>nn||yy<1||yy>mm)
						continue;
					if(mp[xx][yy]==0) 
						match[id[(i-1)*mm+j-1]][id[(xx-1)*mm+yy-1]]=1;  
				}
			}
		}
		int ans;
		printf("%d\n",hungary ());
		for(int i=1;i<=m;i++)
		{
			if(~mark[i])
			{
				int sec=oubiao[i];
				int fir=jibiao[mark[i]];
				printf("(%d,%d)--(%d,%d)\n",fir/mm+1,fir%mm+1,sec/mm+1,sec%mm+1);
			}
        }
		puts("");
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值