HDU 1507 Uncle Tom's Inherited Land* 二分图最大匹配(基础题)

hdu 1507 二分图最大匹配,基础题
相邻节点之间的下标和存在奇偶关系,根据下标和的奇偶性建图。

航电OJ 1507题目链接

Uncle Tom’s Inherited Land*

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

/*---------------hdu 1507    二分图最大匹配,基础题

题意:
    给一个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));//否则会重复出现路径。A-B B-A是重复出现。
        if (DFS(i, j))               //只选择A(奇数)开始,避免B-A情形出现
            ans++;
    }
    return ans;
}

int main(){


    while(~scanf_s("%d%d",&n,&m)){
        if(n==0 && m==0)
            break;
        memset(map,0,sizeof(map));
        scanf_s("%d",&k);
        int u,v;
        while(k--){
            scanf_s("%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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章