HDU 1507 Uncle Tom's Inherited Land*

Uncle Tom's Inherited Land*

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 7
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 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)
 
Source
South America 2002 - Practice
 
 
 
 

题意:
    给定一个n*m的矩阵,去除里面的黑色小方格,两个相邻的白色小方格为一组,问一共能找多少组。
很明显的最大匹配,遍历矩阵的每一个可行点,把它和上下左右的可行点连起来,然后就得到了一个二分图,但这题纠结的地方是如果直接这样建图,每对节点之间就有两条边了,比如(1,2)--(1,3)和(1,3)--(1,2),这两点之间就有两条边,因为这地方卡了我一上午······其实仔细想一下就能发现只要跳过相邻的节点建图就可以了,而相邻节点之间的下标和存在奇偶关系,于是根据下标和的奇偶性建图。
 
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int n,m,k,map[110][110],vis[110][110];

struct node{
    int x,y;
}linker[110][110];

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int DFS(int x,int y){
    for(int i=0;i<4;i++){
        int sx=x+dir[i][0];
        int sy=y+dir[i][1];
        if(sx>=1 && sx<=n && sy>=1 && sy<=m && !vis[sx][sy] && !map[sx][sy]){
            vis[sx][sy]=1;
            if(linker[sx][sy].x==-1 || DFS(linker[sx][sy].x,linker[sx][sy].y)){
                linker[sx][sy].x=x;
                linker[sx][sy].y=y;
                return 1;
            }
        }
    }
    return 0;
}

int Hungary(){
    int ans=0;
    memset(linker,-1,sizeof(linker));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(!map[i][j] && (i+j)&1){      //根据下标和的奇偶性建图
                memset(vis,0,sizeof(vis));
                if(DFS(i,j))
                    ans++;
            }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        if(n==0 && m==0)
            break;
        memset(map,0,sizeof(map));
        scanf("%d",&k);
        int u,v;
        while(k--){
            scanf("%d%d",&u,&v);
            map[u][v]=1;
        }
        int ans=Hungary();
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(linker[i][j].x!=-1)
                    printf("(%d,%d)--(%d,%d)\n",linker[i][j].x,linker[i][j].y,i,j);
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3015488.html