Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1011 Accepted Submission(s): 445
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).
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)
每次在一个白色各自中寻找其上下左右可以匹配的点格子,由于可能有重复匹配,所以算则奇偶的寻找:
View Code
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 int map[110][110]; 7 bool visit[110][110]; 8 struct node{ 9 int x,y; 10 }pre[110][110]; 11 int n,m,k; 12 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 13 14 int find(int x,int y) //找 与位置为(x,y)是否可以连的 还是用匈牙利算法 15 { 16 int i,j,k; 17 for(k=0;k<4;k++) 18 { 19 i=x+dir[k][0]; 20 j=y+dir[k][1]; 21 if(i>=1 && i<=n && j>=1 && j<=m && !visit[i][j] && !map[i][j]) 22 { 23 visit[i][j]=true; 24 if(pre[i][j].x==0 || find(pre[i][j].x,pre[i][j].y)) 25 { 26 pre[i][j].x=x; 27 pre[i][j].y=y; 28 return 1; 29 } 30 } 31 } 32 return 0; 33 } 34 35 int main() 36 { 37 int i,j,ans; 38 int x,y; 39 while(scanf("%d%d",&n,&m) , n|m) 40 { 41 memset(map,0,sizeof(map)); 42 scanf("%d",&k); 43 for(i=1;i<=k;i++) 44 { 45 scanf("%d%d",&x,&y); 46 map[x][y]=1; 47 } 48 ans=0; 49 memset(pre,false,sizeof(pre)); 50 for(i=1;i<=n;i++) 51 { 52 for(j=1;j<=m;j++) 53 { 54 if(!map[i][j] && (i+j)&1) //如果格子相连,必然出现(i+j)为奇数的情况; 55 { 56 memset(visit,false,sizeof(visit)); 57 ans+=find(i,j); 58 } 59 } 60 } 61 printf("%d\n",ans); 62 for(i=1;i<=n;i++) 63 { 64 for(j=1;j<=m;j++) 65 { 66 if(pre[i][j].x) 67 { 68 printf("(%d,%d)--(%d,%d)\n",pre[i][j].x,pre[i][j].y,i,j); 69 } 70 } 71 } 72 } 73 return 0; 74 }