HDU 1507

来源:互联网 发布:上财大数据经济学 编辑:程序博客网 时间:2024/05/22 14:04

[NEW]2013-11月杭电ACM比赛参赛人员信息~ 

Uncle Tom's Inherited Land*

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

Recommend
LL
 

       本来以为这题是把所有可能的小长方形列出来,然后对相互冲突的长方形连一条边,在求一次最大独立点集的。但是在网上看了别人的解法后才知道这题是建一个二分图。具体做法是把格子(i,j)分成两部分,一个是i+j为奇数,另一个为偶数(就像国际象棋棋盘一样)。对于可以构成小长方形的两个格子就连一条边并排除题目中给的特殊点。然后求最大匹配就可以了。

#include<cstdio>#include<cstring>#include<algorithm>#define maxn 109#define maxm 10009using namespace std;int n,m,tot;int first[maxm];int next[maxm],v[maxm];int g[maxn][maxn];bool vis[maxm];int cx[maxm],cy[maxm];int dx[]={-1,0,0,1};int dy[]={0,-1,1,0};void add(int x,int y){    v[tot]=y;    next[tot]=first[x];    first[x]=tot++;}int path(int u){    for(int e=first[u];e!=-1;e=next[e])    {        if(!vis[v[e]])        {            vis[v[e]]=1;            if(cy[v[e]]==-1||path(cy[v[e]]))            {                cy[v[e]]=u;                cx[u]=v[e];                return 1;            }        }    }    return 0;}int match(){    int res=0;    memset(cx,-1,sizeof(cx));    memset(cy,-1,sizeof(cy));    for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    {        if(cx[i*m+j]==-1&&(i+j)%2==0)        {            memset(vis,0,sizeof(vis));            res+=path(i*m+j);        }    }    return res;}bool in(int x,int y){    return x>=0&&x<n&&y>=0&&y<m;}int main(){    while(scanf("%d%d",&n,&m),n+m)    {        int k;        scanf("%d",&k);        memset(g,0,sizeof(g));        memset(first,-1,sizeof(first));        tot=0;        for(int i=0;i<k;i++)        {            int x,y;            scanf("%d%d",&x,&y);            x--,y--;            g[x][y]=1;        }        for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        {            if((i+j)%2==0&&!g[i][j])            {                for(int k=0;k<4;k++)                {                    int x=i+dx[k];                    int y=j+dy[k];                    if(in(x,y)&&!g[x][y])                        add(i*m+j,x*m+y);                }            }        }        printf("%d\n",match());        for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        {            if((i+j)%2==0)            {                if(cx[i*m+j]!=-1)                    printf("(%d,%d)--(%d,%d)\n",i+1,j+1,cx[i*m+j]/m+1,cx[i*m+j]%m+1);            }        }        printf("\n");    }}


原创粉丝点击