码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1507 Uncle Tom's Inherited Land*(二分)

时间:2014-07-26 17:23:52      阅读:580      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   java   color   os   strong   io   

Uncle Tom‘s Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1853    Accepted Submission(s): 769
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). 
bubuko.com,布布扣
 

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)


思路:
编号,构建新图,遍历坐标和为奇数的点,求出最大匹配;
然后,遍历每个点(link数组),如果它有匹配点的话,就将其输出。

#include"stdio.h"
#include"string.h"
#define N 100
#define M 55
int e[N][N],id[N][N];
int n,m,g[N][N],cnt;
int mark[N],link[N];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
struct node         //存可匹配点的横纵坐标
{
    int x,y;
}p[N];
int judge(int x,int y)
{
    if(x>0&&x<=n&&y>0&&y<=m)
        return 1;
    return 0;
}
int find(int k)
{
    int i;
    for(i=0;i<cnt;i++)
    {
        if((p[i].x+p[i].y)%2)
            continue;
        if(!mark[i]&&g[k][i])
        {
            mark[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,k,q,x,y,u,v;
    while(scanf("%d%d",&n,&m),n||m)
    {
        memset(e,-1,sizeof(e));
        memset(g,0,sizeof(g));
        memset(id,0,sizeof(id));
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&u,&v);
            e[u][v]=0;
        }
        cnt=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(e[i][j]==-1)
                {
                    p[cnt].x=i;
                    p[cnt].y=j;
                    id[i][j]=cnt++;
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(e[i][j]==-1)
                {
                    for(k=0;k<4;k++)
                    {
                        x=dir[k][0]+i;
                        y=dir[k][1]+j;
                        if(judge(x,y)&&e[x][y]==-1)
                        {
                            u=id[i][j];
                            v=id[x][y];
                            g[u][v]=g[v][u]=1;
                        }
                    }
                }
            }
        }
        int ans=0;
        memset(link,-1,sizeof(link));
        for(i=0;i<cnt;i++)
        {
            if((p[i].x+p[i].y)%2==0)
                continue;
            memset(mark,0,sizeof(mark));
            ans+=find(i);
        }
        printf("%d\n",ans);
        for(i=0;i<cnt;i++)
        {
            if(link[i]==-1)
                continue;
            j=link[i];
            printf("(%d,%d)--(%d,%d)\n",p[i].x,p[i].y,p[j].x,p[j].y);
        }
        printf("\n");
    }
    return 0;
}


hdu 1507 Uncle Tom's Inherited Land*(二分),布布扣,bubuko.com

hdu 1507 Uncle Tom's Inherited Land*(二分)

标签:des   style   http   java   color   os   strong   io   

原文地址:http://blog.csdn.net/u011721440/article/details/38144629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!