【CF7A Kalevitch and Chess】题解

题目链接

题目

famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards.

As before, the chessboard is a square-checkered board with the squares arranged in a 8×8 8×8 8×8 grid, each square is painted black or white. Kalevitch suggests that chessboards should be painted in the following manner: there should be chosen a horizontal or a vertical line of 8 squares (i.e. a row or a column), and painted black. Initially the whole chessboard is white, and it can be painted in the above described way one or more times. It is allowed to paint a square many times, but after the first time it does not change its colour any more and remains black. Kalevitch paints chessboards neatly, and it is impossible to judge by an individual square if it was painted with a vertical or a horizontal stroke.

Kalevitch hopes that such chessboards will gain popularity, and he will be commissioned to paint chessboards, which will help him ensure a comfortable old age. The clients will inform him what chessboard they want to have, and the painter will paint a white chessboard meeting the client's requirements.

It goes without saying that in such business one should economize on everything — for each commission he wants to know the minimum amount of strokes that he has to paint to fulfill the client's needs. You are asked to help Kalevitch with this task.

原棋盘为8行8列的白色棋盘,每次可将一行或一列染成黑色,问至少需要多少次才能将棋盘染成**样例所示棋盘
输入:目标棋盘状态,W表示白,B表示黑
输出:最少操作次数

思路

由于棋盘很小,所以可以直接枚举每一行每一列染还是不染,然后暴力判断。

可以用二进制搜索来实现。

时间复杂度:\(O(2^{2n}n^2)\)\(n=8\)

总结

一开始这道题我之所以打爆搜以为是翻转,然后果断打爆搜。

然后发现是染色...

这就警示我要看清题目。

另一方面,其实二进制搜索在枚举01情况的时候,如果数据范围不是很大(不过 \(O(2^n)\) 的时间复杂度数据范围大不了多少),打二进制搜索有时候挺好的,不一定要打爆搜嘛~

Code

// Problem: CF7A Kalevitch and Chess
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF7A
// Memory Limit: 62 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k; 
int ans=1e9; 
char s[10][10]; 

int pan(int x)
{
	int i, j, k; 
	for(i=1; i<=8; ++i) 
		for(j=1; j<=8; ++j)
		{
			k=(x & (1<<i-1)) | (x & (1<<(j+7)) ); 
			if(s[i][j]=='W' && k) return 0; 
			if(s[i][j]=='B' && !k) return 0; 
		}
	return 1; 
}

int qiu(int x)
{
	int ans=0; 
	while(x) ++ans, x-=x&-x; 
	return ans; 
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	for(i=1; i<=8; ++i) scanf("%s", s[i]+1); 
	for(i=0; i<(1<<16); ++i) if(pan(i)) ans=min(ans, qiu(i)); 
	printf("%d", ans); 
	return 0; 
}

posted @ 2022-01-13 21:53  zhangtingxi  阅读(93)  评论(0编辑  收藏  举报