HDU 1507 Uncle Tom's Inherited Land*(二分图最大匹配:输出一组解)

来源:互联网 发布:android sdk源码下载 编辑:程序博客网 时间:2024/05/22 08:18

题意:N*M的矩形,向其中填充1*2的小块矩形,黑色的部分不能填充,白色部分需要填充且1*2矩形不能重叠。问最多可以填充多少块。输出任意一组填充解即可.

思路首先同样将原图的所有可填充部分小方格编号,然后将这些合法小方格(可填充)按照他们的行号+列号是奇数还是偶数分为两个点集.(有一个重要的结论,矩阵或者棋盘中,根据行号+列号的奇偶性可以分成一个二分图

           对于两个相邻的合法小方格,他们必定一个属于左边的点集,一个属于右边的点集. 那么在左右点集之间连接一条边.

           最终我们要求的就是该图的最大匹配边。

           输出任意一组解,我们只需要保存二分图节点的原始坐标即可.


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=1000+5;struct Max_Match{    int n,m;    vector<int> g[maxn];    bool vis[maxn];    int left[maxn];    void init(int n,int m)    {        this->n=n;this->m=m;        for(int i=1; i<=n; ++i) g[i].clear();        memset(left,-1,sizeof(left));    }    bool match(int u)    {        for(int i=0;i<g[u].size();++i)        {            int v=g[u][i];            if(!vis[v])            {                vis[v]=true;                if(left[v]==-1 || match(left[v]))                {                    left[v]=u;                    return true;                }            }        }        return false;    }    int solve()    {        int ans=0;        for(int i=1; i<=n; ++i)        {            memset(vis,0,sizeof(vis));            if(match(i)) ++ans;        }        return ans;    }}MM;int cas=1;int mapp[maxn][maxn];int n,m;struct Node{int x,y;Node(){}Node(int x,int y):x(x),y(y){}}node1[maxn],node2[maxn];bool check(int i,int j){if (node1[i].x+1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x-1==node2[j].x && node1[i].y==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y+1==node2[j].y)return true;if (node1[i].x==node2[j].x && node1[i].y-1==node2[j].y)return true;return false;}int main(){int k;    while(scanf("%d%d",&n,&m)!=EOF && n)    {        scanf("%d",&k);memset(mapp,0,sizeof(mapp)); while (k--){int r,c;scanf("%d%d",&r,&c);mapp[r][c]=-1;}int num1=0,num2=0;        for (int i = 1;i<=n;i++)for (int j = 1;j<=m;j++)if (mapp[i][j]==0){if ((i+j)%2==0)node1[++num1]=Node(i,j);elsenode2[++num2]=Node(i,j);}MM.init(num1,num2);for (int i = 1;i<=num1;i++)for (int j = 1;j<=num2;j++)                if (check(i,j)){MM.g[i].push_back(j);}        printf("%d\n",MM.solve());for (int v = 1;v<=num2;v++){int u = MM.left[v];if (u!=-1){printf("(%d,%d)--(%d,%d)\n",node1[u].x,node1[u].y,node2[v].x,node2[v].y);}}printf("\n");    }    return 0;}

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)
 


0 0
原创粉丝点击