ZOJ 1516 Uncle Tom's Inherited Land (二分圖匹配)

題目鏈接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1516

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.


<b< dd="">

Output

For each test case in the input your program should produce one line of output, containing an integer value representing the maximum number of properties which can be sold.


<b< dd="">

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


<b< dd="">

Sample Output

4
3


   題意:n*m的土地上,求有多少1*2 或者 2*1 的塊數 ,池塘不能算在內.

   思路:每一個不是池塘的土地,都可以跟他的上下左右四個方向上不是池塘的塊構成一個1*2或者2*1的長方形塊,把所有連通的塊存起來,構成一個二部圖,剩下的就是求最大匹配.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 110
int g[MAXN][MAXN];
int mp[MAXN][MAXN];
int node[MAXN][MAXN];
int cx[MAXN],cy[MAXN];
int sx[MAXN],sy[MAXN];
int vis[MAXN];
int n,m,num,ans;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
void bulid()
{
    int i,j,k;
    num=1;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
        {
            if(!mp[i][j])
                node[i][j]=num++;
        }
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
        {
            if(!mp[i][j])
            {
                for(k=0; k<4; k++)
                {
                    int tx=i+dir[k][0];
                    int ty=j+dir[k][1];
                    if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&mp[tx][ty]==0)
                    {
                        g[node[i][j]][node[tx][ty]]=1;
                        g[node[tx][ty]][node[i][j]]=1;
                    }
                }

            }
        }
}
int path(int u)
{
    sx[u]=1;
    int v;
    for(v=1; v<=num; v++)
    {
        if(g[u][v]&&!sy[v])
        {
            sy[v]=1;
            if(!cy[v]||path(cy[v]))
            {
                cx[u]=v;
                cy[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int MaxMatch()
{
    int i;
    ans=0;
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    for(i=1; i<=num; i++)
    {
        if(!cx[i])
        {
            memset(sx,0,sizeof(sx));
            memset(sy,0,sizeof(sy));
            ans+=path(i);
        }
    }
    return 0;
}
int main()
{
    int i,j,k,u,v;
    while(~scanf("%d%d",&n,&m)&&n+m)
    {
        memset(mp,0,sizeof(mp));
        memset(g,0,sizeof(g));
        memset(node,0,sizeof(node));
        scanf("%d",&k);
        for(i=0; i<k; i++)
        {
            scanf("%d%d",&u,&v);
            mp[u][v]=1;
        }
        bulid();
        MaxMatch();
        printf("%d\n",ans/2);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章