[HEOI2016/TJOI2016]游戏

Description
在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂。简单的说,这个游戏就是在一张地图上放上若干个炸弹,看是否能炸到对手,或者躲开对手的炸弹。在玩游戏的过程中,小H想到了这样一个问题:当给定一张地图,在这张地图上最多能放上多少个炸弹能使得任意两个炸弹之间不会互相炸到。炸弹能炸到的范围是该炸弹所在的一行和一列,炸弹的威力可以穿透软石头,但是不能穿透硬石头。给定一张nm的网格地图:其中代表空地,炸弹的威力可以穿透,可以在空地上放置一枚炸弹。x代表软石头,炸弹的威力可以穿透,不能在此放置炸弹。#代表硬石头,炸弹的威力是不能穿透的,不能在此放置炸弹。例如:给出14的网格地图xx,这个地图上最多只能放置一个炸弹。给出另一个14的网格地图x#,这个地图最多能放置两个炸弹。现在小H任意给出一张n*m的网格地图,问你最多能放置多少炸弹

Input
第一行输入两个正整数n,m,n表示地图的行数,m表示地图的列数。1≤n,m≤50。接下来输入n行m列个字符,代表网格地图。的个数不超过nm个

Output
输出一个整数a,表示最多能放置炸弹的个数

Sample Input

4 4
#***
*#**
**#*
xxx#

Sample Output
5

考虑如果没有硬石头,那么对于每个能放置炸弹的点,将其所在行向所在列连边即可。如果存在硬石头,则将改行列分成了两个区域,于是我们对这这些区域重新编号,然后连边求最大匹配即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define lowbit(x) ((x)&-(x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=2e3,M=4e3;
int pre[M+10],now[N+10],child[M+10];
int path[N+10],vis[N+10];
int tot,Time;
int row[55][55],col[55][55];//column
char map[55][55];
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool Extra(int x){
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        if (vis[son]==Time) continue;
        vis[son]=Time;
        if (!~path[son]||Extra(path[son])){
            path[son]=x;
            return 1;
        }
    }
    return 0;
}
int main(){
    memset(path,255,sizeof(path));
    int n=read(),m=read(),Row=0,Col=0,Ans=0;
    for (int i=1;i<=n;i++)  scanf("%s",map[i]+1);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if (map[i][j]=='#') continue;
            if (j==1||map[i][j-1]=='#') Row++;
            row[i][j]=Row;
        }
    }
    for (int j=1;j<=m;j++){
        for (int i=1;i<=n;i++){
            if (map[i][j]=='#') continue;
            if (i==1||map[i-1][j]=='#') Col++;
            col[i][j]=Col;
        }
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            if (map[i][j]=='*')
                join(row[i][j],col[i][j]);
    for (int i=1;i<=Row;i++){
        ++Time;
        if (Extra(i))   ++Ans;
    }
    printf("%d\n",Ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Wolfycz/p/10252411.html