集训队专题(6)1005 Uncle Tom's Inherited Land*

来源:互联网 发布:透视变换算法 编辑:程序博客网 时间:2024/04/30 07:50

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2908    Accepted Submission(s): 1206
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 461 11 42 24 14 24 44 344 23 22 23 10 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
 

和大多数二分图的题相似,题目并不是难在求出最大匹配上,而是建立二分图,此题的问题是让我们求出最多的一种用1*2的矩形填涂的方法,也就是说在建立的时候我们要以相邻的作为两个集合,所以我们将相间的格子作为一个集合,不难发现相间的格子横纵坐标之和的奇偶性是一样的,所以在代码实现上我们把横纵坐标之和奇偶行相等的作为一个集合来建图。

#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <vector>using namespace std;const int MAXN = 510;int uN,vN,g[MAXN][MAXN],linker[MAXN];bool used[MAXN];bool dfs(int u){    for(int v = 0; v < vN;v++)        if(g[u][v] && !used[v])        {            used[v] = true;            if(linker[v] == -1 || dfs(linker[v]))            {                linker[v] = u;                return true;            }        }    return false;}int hungary(){    int res = 0;    memset(linker,-1,sizeof(linker));    for(int u = 0;u < uN;u++)    {        memset(used,false,sizeof(used));        if(dfs(u))res++;    }    return res;}int a[110][110];int b[100];int main(){    int n,m,k;    int u,v;    while(scanf("%d%d",&n,&m)==2)    {        if(n == 0 && m == 0)break;        scanf("%d",&k);        memset(a,0,sizeof(a));        while(k--)        {            scanf("%d%d",&u,&v);            u--;v--;            a[u][v] = -1;        }        int index = 0;        for(int i = 0;i < n;i++)            for(int j = 0;j < m;j++)                if(a[i][j]!=-1)                {                    b[index] = i*m + j;                    a[i][j] = index++;                }        uN = vN = index;        memset(g,0,sizeof(g));        for(int i = 0;i < n;i++)            for(int j= 0;j < m;j++)                if(a[i][j]!=-1 && (i+j)%2==1)                {                    u = a[i][j];                    if(i > 0 && a[i-1][j]!=-1)                        g[u][a[i-1][j]]=1;                    if(i < n-1 && a[i+1][j]!=-1)                        g[u][a[i+1][j]]=1;                    if(j > 0 && a[i][j-1]!=-1)                        g[u][a[i][j-1]]=1;                    if(j < m-1 && a[i][j+1]!=-1)                        g[u][a[i][j+1]]=1;                }        int ans = hungary();        printf("%d\n",ans);        for(int i = 0;i <vN;i++)            if(linker[i]!=-1)            {                int x1 = b[i]/m;                int y1 = b[i]%m;                int x2 = b[linker[i]]/m;                int y2 = b[linker[i]]%m;                printf("(%d,%d)--(%d,%d)\n",x1+1,y1+1,x2+1,y2+1);            }        printf("\n");    }    return 0;}


0 0