LibreOJ#6030. 「雅礼集训 2017 Day1」矩阵

https://loj.ac/problem/6030

如果矩阵第i列有一个黑色,

那可以用他把第i行全都染黑,也可以使任意一列具有黑色

然后就可以用第i行把矩阵染黑

染黑一列的代价最少是1

染黑一行的代价最少是 白点数+(这一列是否有黑色)

如果没有黑色的话还需要1的代价 使这一列有黑色

#include<cstdio>
#include<algorithm>
#include<iostream>

using namespace std;

#define N 1001

int h[N],l[N];

char s[N];

int main()
{
    int n;
    scanf("%d",&n);
    bool ok=false;
    for(int i=1;i<=n;++i)
    {
        scanf("%s",s+1);
        for(int j=1;j<=n;++j)
            if(s[j]=='#') 
            {
                ok=true;
                h[i]++;
                l[j]++;
            }
    }
    if(!ok) 
    {
        puts("-1");
        return 0;
    }
    int ans=n;
    for(int i=1;i<=n;++i) ans=min(ans,n-h[i]+!l[i]);
    int sum=0;
    for(int i=1;i<=n;++i) sum+=l[i]!=n; 
    cout<<ans+sum;
}
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8144856.html