HDU_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): 2208    Accepted Submission(s): 913
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)
 
题意:给你一个N*M的矩阵,里面有些点是不能用的,现在问你用1*2的方格最多能放多少,不能重叠,并且输出每对方格坐标。
分析:这道题很明显的就是二分图最大匹配问题,就想着相邻的两个方格连一条边,但是这样呢每对方格就存在两条边了。想了很久啊就是不知道怎么处理,蒟蒻表示没想出办法只能看题解了。在建图的时候只要根据下标之和的奇偶性建图就好了,这样子就不会每对方格存在两条边了。从此题学到了奇偶性建图,值得深刻理解。
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1507
代码清单:
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxv = 100 + 5;

struct Edge{
    int x;
    int y;
}edge[maxv];

int N,M;
int K,X,Y;
int cnt,sum;
int match[maxv];
bool vis[maxv];
int num[maxv][maxv];
bool graph[maxv][maxv];
bool trueGraph[maxv][maxv];

void init(){
    cnt=0; sum=0;
    memset(num,0,sizeof(num));
    memset(match,-1,sizeof(match));
    memset(graph,true,sizeof(graph));
    memset(trueGraph,false,sizeof(trueGraph));
}

void getGraph(int x,int y){
    if(x-1>=1&&graph[x-1][y]) trueGraph[num[x][y]][num[x-1][y]]=true;
    if(x+1<=N&&graph[x+1][y]) trueGraph[num[x][y]][num[x+1][y]]=true;
    if(y-1>=1&&graph[x][y-1]) trueGraph[num[x][y]][num[x][y-1]]=true;
    if(y+1<=M&&graph[x][y+1]) trueGraph[num[x][y]][num[x][y+1]]=true;
}

void mem(){
     for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            if(graph[i][j]){
                num[i][j]=++cnt;
                edge[cnt].x=i;
                edge[cnt].y=j;
            }
        }
    }
    for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
            if(graph[i][j]&&((i+j)&1)) getGraph(i,j);
}

bool dfs(int u){
    for(int v=1;v<=cnt;v++){
        if(!vis[v]&&trueGraph[u][v]){
            vis[v]=true;
            if(match[v]==-1 || dfs(match[v])){
                match[v]=u;
                return true;
            }
        }
    }return false;
}

void getMap(){
    for(int u=1;u<=cnt;u++){
        memset(vis,false,sizeof(vis));
        if(dfs(u)) sum++;
    }
}

void print(){
    printf("%d\n",sum);
    for(int i=1;i<=cnt;i++){
        int j=match[i];
        if(match[i]!=-1){
            printf("(%d,%d)--(%d,%d)\n",edge[i].x,edge[i].y,edge[j].x,edge[j].y);
        }
    }
}

void doit(){
    mem();
    getMap();
    print();
}

int main(){
    while(scanf("%d%d",&N,&M)!=EOF){
        if(N==0&&M==0) break;
        init();
        scanf("%d",&K);
        for(int i=0;i<K;i++){
            scanf("%d%d",&X,&Y);
            graph[X][Y]=false;
        }
        doit();
    }return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值