Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1853 Accepted Submission(s): 769
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).
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).
![](https://i-blog.csdnimg.cn/blog_migrate/59be5e7b0ceb44fb26011da271f849a5.gif)
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)
思路:黑白染色法,把(i+j)是否为奇偶作为黑白的判断依据;
代码如下:
#include<stdio.h>
#include<string.h>
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int a,b,n,m;
int map[110][110];
int link[110];
int visit[110];
int match[110][110];
struct rep
{
int x,y;
}rep[110];
void search(int w)
{
int i;
int nx,ny;
for(i=0;i<4;i++)
{
nx=rep[w].x+dir[i][0];
ny=rep[w].y+dir[i][1];
if(nx<1||ny<1||nx>n||ny>m||map[nx][ny]==0) continue;
match[w][map[nx][ny]]=1;;
}
}
int find(int x)
{
int i;
for(i=1;i<=a;i+=2)
{
if(!visit[i]&&match[i][x])
{
visit[i]=1;
if(!link[i]||find(link[i]))
{
link[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
memset(map,-1,sizeof(map));
memset(match,0,sizeof(match));
memset(link,0,sizeof(link));
int k;
scanf("%d",&k);
while(k--)
{
int u,v;
scanf("%d %d",&u,&v);
map[u][v]=0;
}
a=1; //把所有奇数的rep当成黑
b=2; //把所有偶数的rep当成白
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if((i+j)%2==0)
{
if(map[i][j]==-1)
{
map[i][j]=a;
rep[a].x=i;
rep[a].y=j;
a+=2;
}
}
else
{
if(map[i][j]==-1)
{
map[i][j]=b;
rep[b].x=i;
rep[b].y=j;
b+=2;
}
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if((i+j)%2==0&&map[i][j]) //如果一块骨牌可以存在于黑白之间,就match
{
search(map[i][j]);
}
}
}
int sum=0;
for(i=2;i<=b;i+=2)
{
memset(visit,0,sizeof(visit));
if(find(i))
sum++;
}
printf("%d\n",sum);
for(i=1;i<=a;i+=2)
{
if(link[i])
{
printf("(%d,%d)--(%d,%d)\n",rep[i].x,rep[i].y,rep[link[i]].x,rep[link[i]].y);
}
}
}
return 0;
}