杭电HDU ACM 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): 2496    Accepted Submission(s): 1028
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)
 

Source
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1281  1068  1528  1151  1150
题目意思:
给定一个  n * m 的矩阵格子, 第二行给定一个k值表示池塘,接下来给出池塘坐标,要求的就是在不是池塘的区域用1 * 2 的小矩形填充它 ,问 最多能填充几个小矩形。

这个是昨天左右让我看的一个题目,说他怎么也找不到他的bug ,先来说说我的代码,将二分图所有点按照 各点横纵坐标加和的奇偶性来进行划分。因为如果一个小格子为奇数,那么他周围相邻格子必然为偶数。也就是说只有相邻的具有奇偶两者的格子才构成了1 * 2小矩形,那么此时就符合了二分图意义,实质上就是用二分图最大匹配求最多有多少个奇偶连边,这些边保证了不会交叉。

为了方便建图,将所有不是陆地部分从1……num……开始标记,然后枚举任意一个非池塘点,搜索其周围,用G数组保存二分图,那么注意G数组大小最大可能为n * m,或着直接用num - 1的值,也就是陆地小格子的个数。对于如何输出匹配边,用一个二维数组或者一个结构体pre【】【】表示dic数组中某点值的横纵坐标,那么对于所有的偶数格子如果有匹配的前提下,即from【i】!=  -1,而等于i 点的右匹配点的值,然后通过pre数组查询其横纵坐标即可。

详情看代码:
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-27 10:36
#
#     Filename: E.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/

#include<iostream>    
#include<sstream>    
#include<algorithm>    
#include<cstdio>    
#include<string.h>    
#include<cctype>    
#include<string>    
#include<cmath>    
#include<vector>    
#include<stack>    
#include<queue>    
#include<map>    
#include<set>    
using namespace std;
const int INF=201; 
int cnt[INF][INF];
int dic[INF][INF];
int from[INF],tot;
int pre[INF][2];
bool use[INF];
int dir[][2] = {{1,0},{0,1},{0,-1},{-1,0}};
vector<int>G[INF * INF];
    int n, m;
    int num ;

bool match(int x)
{
     for(int i=0;i<G[x].size();i++)
    {

         if(!use[G[x][i]])
        {
             use[G[x][i]]=1;
            if(from[G[x][i]]==-1||match(from[G[x][i]]))
            {
                 from[G[x][i]]=x;
                return 1;
            }
        }
    }
    return 0;
}

int hungary( )
{
     tot=0;
    memset(from,255,sizeof(from));
    for(int i=1;i<= num - 1;i++)
    {
         memset(use,0,sizeof(use));
        if(match(i))
           {
               ++tot;
           }
    }
    return tot;
}

int main(){
    while(scanf("%d%d",&n, &m) != EOF && n + m){
        int k ;scanf("%d",&k);
        memset(dic, 0, sizeof(dic));
        memset(cnt, 0, sizeof(cnt));
        memset(pre, 0, sizeof(pre));
        for(int i = 1; i <= k; i++){
            int x, y;
            scanf("%d%d",&x, &y);
            cnt[x][y] = 1;
        }
         num = 1;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <=m; j++){
                if(!cnt[i][j]){
                    pre[num][0] = i;
                    pre[num][1] = j;
                    dic[i][j] = num++; 
                    
                }
            }
        }
      
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(dic[i][j])
                    if(((i + j) & 1))
                    for(int k = 0; k < 4; k++)
                    {
                        int x = i + dir[k][0];
                        int y = j + dir[k][1];
                        if(x >= 1 && x <= n && y >=1 && y <=m && dic[x][y])
                            G[dic[i][j]].push_back(dic[x][y]);
                    }

            }
        }
        int ans = hungary();
        cout<<ans<<endl;
        for(int i = 1; i <= num - 1; i++){
           if(from[i] != -1){
            cout<<"("<<pre[i][0]<<","<<pre[i][1]<<")--"
            <<"("<<pre[from[i]][0]<<","<<pre[from[i]][1]<<")\n";

            }
        
        }
        for(int i = 1; i <= INF * INF; i++)
            G[i].clear();
        cout<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值