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): 3624    Accepted Submission(s): 1532
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)


奇偶建图就是将一个点与他四周的四个点标记成不一样(一奇一偶或一偶一奇),这样建出来的图刚好是二分图(奇一列,偶一列)。再将一个点与他可以到达的点

建边这样一张二分图就建好了。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MAXN 60

typedef struct local
{
	int x,y;
}loca;

typedef struct Newmap
{
	int value;
	int num;
}newmap;

loca ou[MAXN],ji[MAXN];
int B[MAXN],f[MAXN];    //B[i]表示i奇数格B[i]偶数格
int map[MAXN][MAXN];   //map[x][y]表示x是偶数点y是奇数点
newmap graph[110][110];

int n,m,num1,num2,oun,jin;

int Hungary(int k,int B[])   //k是(i+j)为偶数的格子
{
	int i;
	for(i=1;i<=num2;i++)         //i是(i+j)为奇数的格子
	{
		if(map[k][i]&&f[i]==0)
		{
			f[i]=1;
			if(B[i]==-1||Hungary(B[i],B))
			{
				B[i]=k;
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	int k,i,match,j,c,a,b;
	int ans[51],num;
	while(scanf("%d%d",&n,&m),n+m)
	{
		cin>>k;
		memset(graph,0,sizeof(graph));
		for(i=0;i<k;i++)
		{
			cin>>a>>b;
			graph[a][b].value=1;
		}
		num1=num2=0;
		for(i=1;i<=n;i++)       
		{
			for(j=1;j<=m;j++)
			{
				if(graph[i][j].value==0)   //先将每一个都标记各自在奇偶中的编号
				{
					if((i+j)%2==0)
					{
						++num1;
						graph[i][j].num=num1;
						ou[num1].x=i;
						ou[num1].y=j;
					}
					else
					{
						++num2;
						graph[i][j].num=num2;
						ji[num2].x=i;
						ji[num2].y=j;
					}
				}
			}
		}
		memset(map,0,sizeof(map));
		for(i=1;i<=n;i++)                  //奇偶建图
		{
			for(j=1;j<=m;j++)
			{
				if(graph[i][j].value==0)
				{
					if((i+j)%2==0)
					{
						int x=graph[i][j].num;
						int y;
						if(i-1>=1&&graph[i-1][j].value==0)  //上
						{
							y=graph[i-1][j].num;

							map[x][y]=1;
						}
						if(i+1<=n&&graph[i+1][j].value==0)  //下
						{
							y=graph[i+1][j].num;
							map[x][y]=1;
						}
						if(j-1>=1&&graph[i][j-1].value==0)  //左
						{
							y=graph[i][j-1].num;
							map[x][y]=1;
						}
						if(j+1<=m&&graph[i][j+1].value==0)  //右
						{
							y=graph[i][j+1].num;
							map[x][y]=1;
						}
					}
					else
					{
						int y=graph[i][j].num;
						int x;
						if(i-1>=1&&graph[i-1][j].value==0)  //上
						{
							x=graph[i-1][j].num;

							map[x][y]=1;
						}
						if(i+1<=n&&graph[i+1][j].value==0)  //下
						{
							x=graph[i+1][j].num;
							map[x][y]=1;
						}
						if(j-1>=1&&graph[i][j-1].value==0)  //左
						{
							x=graph[i][j-1].num;
							map[x][y]=1;
						}
						if(j+1<=m&&graph[i][j+1].value==0)  //右
						{
							x=graph[i][j+1].num;
							map[x][y]=1;
						}
					}
				}
					
				
			}

		}
		memset(B,-1,sizeof(B));
		match=0;
		for(i=1;i<=num1;i++)
		{
			memset(f,0,sizeof(f));
			if(Hungary(i,B))
			{
				match++;
			}
		}
		printf("%d\n",match);
		for(i=1;i<=num2;i++)
		{
			if(B[i]!=-1)
			printf("(%d,%d)--(%d,%d)\n",ji[i].x,ji[i].y,ou[B[i]].x,ou[B[i]].y);
		}
		printf("\n");
	}
	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值