ZOJ 1516--Uncle Tom's Inherited Land【二分图 && 求最大匹配数 && 经典建图】

来源:互联网 发布:itunes软件备份路径 编辑:程序博客网 时间:2024/05/22 00:17

Uncle Tom's Inherited Land

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 produce one line of output, containing an integer value representing the maximum number of properties which can be sold.


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
3

题意:

Tom有块 n * m个方格的土地,上面有 k 个方格水池,其余的为空地,Tom想把土地卖了,但有两点要求:

(1)水池不能卖。

(2)两块空地组成的(1 * 2 或 2 * 1)长方形土地才能出售。

问Tom最多可以出售多少块长方形土地。


解题:

给每一块空地标号,每个空地当做一个顶点,我们知道相邻的两块空地可以组成出售的长方形土地。所以枚举每一块空地, 搜索这块空地的上下左右相邻的四个位置还有没有空地,如果有的话建边,这样就构成了一个二分图模型,求出最大匹配数 ,这是符合出售条件的空地,还需要 / 2,才是可以出售的长方形土地的个数。

#include <cstdio>#include <cstring>#include <algorithm>#define maxn 120using namespace std;int n, m, k;int ans, sum;int vis[maxn][maxn];int map[maxn][maxn];int node[maxn][maxn];int used[maxn];int link[maxn];int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};void init(){    memset(vis, 0, sizeof(vis));    memset(map, 0, sizeof(map));    memset(node, 0, sizeof(node));}void getmap(){    while(k--){        int a, b;        scanf("%d%d", &a, &b);        vis[a][b] = 1;//1代表水池    }    ans = 0;    //给每个空地编号    for(int i = 1; i <= n; ++i)    for(int j = 1; j <= m; ++j){        if(!vis[i][j])            node[i][j] = ++ans;    }    //找每块空地的上下左右相邻的位置,如果相邻的也是空地就建边    for(int i = 1; i <= n; ++i){        for(int j = 1; j <= m; ++j){            if(vis[i][j]) continue;            for(int h = 0; h < 4; ++h){                int nx, ny;                nx = i + dir[h][0];                ny = j + dir[h][1];                if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny])                    map[node[i][j]][node[nx][ny]] = 1;                    map[node[nx][ny]][node[i][j]] = 1;            }        }    }}bool dfs(int x){    for(int i = 1; i <= ans; ++i){        if(map[x][i] && !used[i]){            used[i] = 1;            if(link[i] == -1 || dfs(link[i])){                link[i] = x;                return true;            }        }    }    return false;}void hungary(){    sum = 0;    memset(link, -1, sizeof(link));    for(int i = 1; i <= ans; ++i){        memset(used, 0, sizeof(used));        if(dfs(i))            sum++;    }}int main (){    while(scanf("%d%d", &n, &m), n || m){        scanf("%d", &k);        init();        getmap();        hungary();        printf("%d\n", sum / 2);    }    return 0;}


1 0
原创粉丝点击