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
South America 2002 - Practice
刚开始 我算出双向的二分图 结果数目对了 下面匹配不对,最后用奇偶性才对;
把 奇数放一边,偶数另一边,匹配都是奇偶匹配
一下 我的代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define maxn 550
bool Map[maxn][maxn];
bool chart[maxn][maxn];
int Mark[maxn];
bool Flag[maxn];
struct ele
{
int x;
int y;
}lx[maxn],ly[maxn];
bool Find(int x,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(!chart[x][i]||Flag[i])
continue;
Flag[i]=true;
if(!Mark[i]||Find(Mark[i],n))
{
Mark[i]=x;
return true;
}
}
return false;
}
int main()
{
int n,m;
int Count;
int i,j,k;
int x,y,z;
while(scanf("%d%d",&n,&m),n+m)
{
Count=0;
memset(Map,false,sizeof(Map));
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
scanf("%d",&k);
for(i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
Map[x][y]=true;
}
k=z=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(!Map[i][j])
{
if((i+j)%2==0)
{
lx[z].x=i;
lx[z].y=j;
z++;
}
else
{
ly[k].x=i;
ly[k].y=j;
k++;
}
}
int t;
memset(chart,0,sizeof(chart));
for(i=1;i<=z-1;i++)
for(j=1;j<=k-1;j++)
{
t=abs(lx[i].x-ly[j].x)+abs(lx[i].y-ly[j].y);
if(t==1)
chart[i][j]=true;
}
memset(Mark,0,sizeof(Mark));
for(i=1;i<=z-1;i++)
{
memset(Flag,false,sizeof(Flag));
if(Find(i,k-1))
Count++;
}
printf("%d\n",Count);
int x1,y1,x2,y2;
memset(chart,false,sizeof(chart));
for(i=1;i<=k-1;i++)
{
if(Mark[i])
{
x1=ly[i].x;
y1=ly[i].y;
x2=lx[Mark[i]].x;
y2=lx[Mark[i]].y;
printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2);
}
}
printf("\n");
}
return 0;
}