hdoj 1507 Uncle Tom's Inherited Land* 【二分图 二维坐标划分奇偶集构图 求最大匹配】



Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2507    Accepted Submission(s): 1036
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). 
hdoj 1507 Uncle Tom's Inherited Land* 【二分图 二维坐标划分奇偶集构图 求最大匹配】_第1张图片
 

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)
 



憋了好久,终于1A了。


题意:有一个N*M的土地,里面有K个池塘,已经给出池塘坐标。现在要买掉一部分土地,一块土地能卖出的要求——必须是1*2(2*1)的矩形且不能被池塘割断。问你给出的土地最多能够卖出的块数,并输出组成每块的两个坐标。


思路:按坐标奇偶性划分两个集合S和T,集合S向集合T(可以任选,T集指向S集也是可以的)建单向边构图,然后枚举集合S(若是建边T集指向S集,则枚举T集)的坐标求最大匹配。


本题需要用二维数组来实现整个匹配过程。1,记录可匹配的坐标;2,记录匹配的坐标。确实有点麻烦。。。



AC代码;

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define MAXN 101
using namespace std;
int N, M, K;
bool used[MAXN][MAXN];
int Map[MAXN][MAXN];
struct Node
{
    int x[4], y[4];
    int p;//记录当前坐标可以匹配的坐标数目
};
Node G[MAXN][MAXN];
struct rec
{
    int x, y;
}match[MAXN][MAXN];//用结构体存储匹配的坐标
bool judge(int x, int y)//是否越界
{
    return x >= 1 && x <= N && y >= 1 && y <= M && Map[x][y] == 0;
}
void getMap()
{
    memset(Map, 0, sizeof(Map));
    scanf("%d", &K);
    int a, b;
    while(K--)
    {
        scanf("%d%d", &a, &b);
        Map[a][b] = 1;
    }
    int move[4][2] = {0,1, 1,0, 0,-1, -1,0};
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= M; j++)
        {
            match[i][j].x = match[i][j].y = -1;//初始化match
            if(Map[i][j] == 0 && (i+j) & 1)//奇数指向偶数
            {
                G[i][j].p = 0;
                for(int k = 0; k < 4; k++)//4个方向
                {
                    int x = i + move[k][0];
                    int y = j + move[k][1];
                    if(judge(x, y) && (x+y) % 2 == 0)//可以匹配 且坐标和是偶数
                        G[i][j].x[G[i][j].p] = x, G[i][j].y[G[i][j].p++] = y;//记录
                }
            }
        }
    }
}
int DFS(int x, int y)
{
    for(int i = 0; i < G[x][y].p; i++)
    {
        int a = G[x][y].x[i];
        int b = G[x][y].y[i];
        if(!used[a][b])
        {
            used[a][b] = true;
            if(match[a][b].x == -1 || DFS(match[a][b].x, match[a][b].y))
            {
                match[a][b].x = x;
                match[a][b].y = y;

                return 1;
            }
        }
    }
    return 0;
}
void solve()
{
    int ans = 0;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= M; j++)
        {
            if((i+j) & 1 && !Map[i][j])//跳过坐标和为偶数的集合 跳过池塘
            {
                memset(used, false, sizeof(used));
                ans += DFS(i, j);
            }
        }
    }
    printf("%d\n", ans);
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= M; j++)
        {
            if(match[i][j].x != -1)
                printf("(%d,%d)--(%d,%d)\n", i, j, match[i][j].x, match[i][j].y);
        }
    }
}
int main()
{
    while(scanf("%d%d", &N, &M), N||M)
    {
        getMap();
        solve();
    }
    return 0;
}


你可能感兴趣的:(hdoj 1507 Uncle Tom's Inherited Land* 【二分图 二维坐标划分奇偶集构图 求最大匹配】)