POJ1358解析

Description

The Ministry of housing is planning a huge construction project of several housing complexes. Each complex includes several apartments to be sold to government employees at reasonable prices. The ministry has located several big m*n pieces of land that can potentially be used for such construction project; one complex in each land. The lands are all rectangular, each with m*n number of 1*1 square blocks. All housing complexes are h*w rectangles covering exactly h*w blocks of each containing land. 

The problem is that there are originally some old buildings, each covering exactly one block of a land, making it impossible to locate enough free space for all the complexes in order to start the project. Therefore, the ministry has to buy some of these buildings, and demolish them to free the needed space. The old buildings belong to certain number of people. These people are angry of the possibility that their building may be bought and demolished, especially because the government usually pays much less for their buildings compared to the open market prices. 

In response to the protests, the ministry announces a "fair" decision that if it buys some buildings in one land, it will only choose those that belong only to one owner, and will buy all of them at reasonable price. And, it promises not to buy buildings belonging to the same owner in other lands. Note that with this constraint, there may be some lands in which building a complex is impossible. Trying to keep its promises, the ministry has asked you to write a program to see how many housing complexes can be constructed at most with these conditions. 
 

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains five integers k (1 <= k <= 30), the number of lands, m and n (1 <= m, n <= 50), the number of rows and columns in each land respectively, and h and w (1 <= h, w <= 50), the number of rows and columns a complex occupies. After the first line, there are k*m lines in the input, representing k lands, each by an m*n matrix. Each line contains a string of length n with no leading or trailing spaces. Each character in the strings represents a block in the land and may be an upper case alphabetic character 'A'..'Z', indicating the owner of the block, or the character '0' indicating the block is free.

Output

There should be one line per test case containing the maximum number of housing complexes that can be constructed for that test case. 

Sample Input

2 
3 4 3 3 2 
A0B 
000 
0A0 
00B 
AA0 
00B 
0B0 
000 
A0A 
000 
B00 
B00 
3 4 3 3 2 
A0B 
000 
0A0 
00B 
AA0 
00B 
0B0 
000 
A0A 
000 
0B0 
B00 

Sample Output

3
2 

Source

Tehran 2002

 

题目大意:

给你K个岛屿,每个岛屿的大小是n*m的,一个岛屿最多只能建立一个h*w的房子,岛屿上0的位子是可以直接用于建筑房子的,A~Z表示一些人拥有的地盘,现在政府可以对于一个岛屿,买下一个人拥有的所有地盘(但是其他岛屿就不能再买这个人的地盘了),然后在建立房子。

问最多可以建立多少房子。

 

思路:
 

1、问题肯定在于建立房子的最多数量上来,每个岛屿可以有多种选择,对于每个岛屿的选择可能会影响其他岛屿的选择情况,那么我们想要最优数量,显然最大二分匹配问题。

 

2、那么O(k*26*n*m)枚举每个岛屿想要买的地盘的主人的编号,然后二维前缀和维护这个岛屿买下了这个主人所拥有的地盘之后能否有一个h*w的全0子矩阵。

建好图之后跑一遍最大二分匹配匈牙利算法即可。

注意,如果一个岛屿如果不用购买某人的地盘就能够建成,那么这个岛屿就不用参与二分匹配了,直接output++即可。

 

3、本题有一个trick点,我们只能建立h*w的子矩阵,而不能建立w*h的子矩阵。

 

// 725_POJ_1358.cpp : Defines the entry point for the console application.
//
/*
题意:给出K个n*m的空地,字母A-Z表示障碍。
对于每一块,你可以将某一种字母全部拿走,使得出现一个h*w的空地。
但是每一种字母在一块中被拿走在另一块中就不允许拿这种字母了。求K块中最多有多少块可以出现多少h*w的空地?
*/


#include "stdafx.h"
#include<stdio.h>
#include<string>
using namespace std;
char a[65][65][65];
int mp[65][65];
int sum[65][65];
int match[65];
int vis[65];//是否vis
int p,n,m,h,w;
int k;
int Slove(int kk,int ch){
    memset(sum,0,sizeof(sum));//初始化
    int i,j;
    for( i=1;i<=n;i++){
        for( j=1;j<=m;j++){//n*m 方块
            if(a[kk][i][j]-'A'+1==ch||a[kk][i][j]=='0'){//
                sum[i][j]=1;//有地方,0或者j字节的
            }else{
                sum[i][j]=0;
            }
        }
    }
    for(int ii=1;ii<=n;ii++){
        for(int jj=1;jj<=m;jj++){
            sum[ii][jj]+=sum[ii][jj-1];//代表总个数
        }
    }
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            sum[j][i]+=sum[j-1][i];//代表总个数
        }
    }
    //扫描sum ,sum 代表i,j 

    for(i=1;i<=n;i++)
    {
        for( j=1;j<=m;j++)
        {
            if(i>=h&&j>=w)//够判断的资格,扫描
            {
                /*
                
                哈哈哈真有意思
                见多了就加上


                */
                int x1=i-h;
                int y1=j-w;
                if(sum[i][j]-sum[i-h][j]-sum[i][j-w]+sum[x1][y1]>=h*w)return 1;//可以理解为分割见图


            }
        }
    }
return 0;
}
//二分法
int find(int u){
    for(int j=1;j<=26;j++){
        if(mp[u][j]==1&&vis[j]==0){//u,j代表可以 j=0代表空白
           vis[j]=1;//vis[j] 访问
           if(match[j]==-1||find(match[j])){//二分法 find(match[j])如果可以匹配
                match[j]=u;//u->j 
                return 1;
           }
        }
    }
    return 0;
}
int main(int argc, char* argv[])
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(mp,0,sizeof(mp));
        memset(match,-1,sizeof(match));
        scanf("%d%d%d%d%d",&k,&n,&m,&h,&w);
        for(int i=1;i<=k;i++){
            for(int j=1;j<=n;j++){
                scanf("%s",a[i][j]+1);
            }
            
        }
        for(int s=1;s<=k;s++){
            for(int j=0;j<=26;j++){
                mp[s][j] =Slove(s,j);//统计每个岛屿不同人的数量,二分
            }
        }
        int output =0;
        for(int ii=1;ii<=k;ii++){//对每个岛屿来说,如果满足条件,空地数量满足条件否则就进行搜索
            if(mp[ii][0]==1) output++;
            else{
            memset(vis,0,sizeof(vis));
            if(find(ii)==1) output++;
            }
        
        }

        printf("%d\n",output);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值