loj#6030 「雅禮集訓 2017 Day1」矩陣 貪心

Description


給一個n*n的01矩陣,每次可以用一行替換一列。問最少多少次操作使得整個矩陣全1
n<=1000

Solution


先考慮怎麼把一整行刷成1。我們枚舉全1的行設爲x,若存在第x列爲1的行則可以填上第x行的0,否則我們可以多操作一次任意選一個存在1的行使得某行的第x列爲1,然後照做就行了

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>

#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=2005;

char s[N][N],t[N][N];

int c[N];

int main(void) {
	int n; scanf("%d",&n);
	bool flag=false;
	rep(i,1,n) {
		scanf("%s",s[i]+1);
		rep(j,1,n) {
			if (s[i][j]=='#') c[j]++,flag=true;
		}
	}
	int tot=n;
	rep(i,1,n) if (c[i]==n) tot--;
	int ans=0x3f3f3f3f;
	rep(i,1,n) {
		int cnt=0;
		rep(j,1,n) if (s[i][j]=='.') cnt++;
		ans=std:: min(ans,cnt+tot+(!c[i]?flag:0));
	}
	if (!flag) ans=-1;
	printf("%d\n", ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章