hdu 1507 Uncle Tom's Inherited Land* 黑白棋盘 二分匹配

来源:互联网 发布:天下三捏脸大赛数据 编辑:程序博客网 时间:2024/06/05 11:42

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2170    Accepted Submission(s): 897
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)
 



题意:问未标记为水潭的点最多可以放多少1*2的方块。

把棋盘的每个 点横坐标和纵坐标相加,奇数点放二分图左侧,偶数点放右侧。然后历遍这个棋盘,把可以摆1*2的两个点进行匹配。然后最大匹配就是答案了。



#include <stdio.h>#include <string.h> int dir[4][2]={1,0,0,1,-1,0,0,-1};int mp[110][110];int id[210*210];//地图点 映射 去离散化后的编号int jibiao[150];//编号 映射 地图的点int oubiao[150];#define N 150int visit[N];int mark[N];int match[N][N];int n,m,k;int dfs(int x){    int i;    for(i=1;i<=m;i++)//对左边的节点x与右边的节点进行逐一检查    {        if(!visit[i]&&match[x][i])        {            visit[i]=1;//标记检查过的点            if(mark[i]==-1||dfs(mark[i]))             {//|| 前面过了 后面不运行;;                 mark[i]=x;//修改匹配关系                   return 1;            }        }    }    return 0;}int hungary (){    memset(mark,-1,sizeof(mark));int max=0,j;    for(j=1;j<=n;j++)//对做部分顶点逐个进行遍历{memset(visit,0,sizeof(visit));if(dfs(j))max++;}return max;} int main(){ int num,x,y;int nn,mm;while(scanf("%d%d",&nn,&mm),nn&&mm)//注意 mark[mm]=nn  mark[j] = i{scanf("%d",&num);memset(mp,0,sizeof mp);for(int i=0;i<num;i++){scanf("%d%d",&x,&y);mp[x][y]=1;//水塘}int ji=1;int ou=1;memset(match,0,sizeof(match));for(int i=1;i<=nn;i++){for(int j=1;j<=mm;j++){if(mp[i][j]==0&&((i+j)&1)){jibiao[ji]=(i-1)*mm+j-1;id[(i-1)*mm+j-1]=ji++;}else if(mp[i][j]==0){oubiao[ou]=(i-1)*mm+j-1;id[(i-1)*mm+j-1]=ou++;}}}n=ji-1;//去离散化后  二分图中的n和mm=ou-1;for(int i=1;i<=nn;i++){for(int j=1;j<=mm;j++){if((!mp[i][j])&&(i+j)%2==1)for(int k=0;k<4;k++){int xx,yy;xx=i+dir[k][0];yy=j+dir[k][1];if(xx<1||xx>nn||yy<1||yy>mm)continue;if(mp[xx][yy]==0) match[id[(i-1)*mm+j-1]][id[(xx-1)*mm+yy-1]]=1;  }}}int ans;printf("%d\n",hungary ());for(int i=1;i<=m;i++){if(~mark[i]){int sec=oubiao[i];int fir=jibiao[mark[i]];printf("(%d,%d)--(%d,%d)\n",fir/mm+1,fir%mm+1,sec/mm+1,sec%mm+1);}        }puts("");}return 0;}






0 0