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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章