CF上的一道简单思维题——Kalevitch and Chess

题干:

A 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×88×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.

输入格式

The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements, W character stands for a white square, and B character — for a square painted black.

It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row).

输出格式

Output the only number — the minimum amount of rows and columns that Kalevitch has to paint on the white chessboard to meet the client's requirements.

意思就一个8*8的棋盘,一开始全白,你可以选择一行或者一列全部涂黑,现在给你一张棋盘上各个格子的颜色表,问你要达到这种状态至少需要多少步。

样例输入:

WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW

样例输出:

3

容易想到,如果某行有一个白色块,那么必然这一行不会全涂黑,意思就是没有选择这一行染色。列和行也是一样,如果某列有一个白色块,那么必然这一列不会全涂黑,所以并没有选择这一列染色。于是就得出了一个暴力思路——先检验每一行的颜色,如果都黑那么让步数加一,有一个白就立即遍历下一行。然后再检测列,具体方法和检验行的一致。

最后有一个特殊的情况需要判断,就是棋盘全黑时,检测完行就不需要再检测列了。这说明当检验完行数发现步数已经为8时,可以直接跳过列的检验,直接输出答案。

最后附上代码:

#include<bits/stdc++.h>
using namespace std;
char a[10][10];
int sum,f;
int main()
{
    for(int i=1;i<=8;i++)
    for(int j=1;j<=8;j++)
    cin>>a[i][j];
    for(int i=1;i<=8;i++)
    {
        for(int j=1;j<=8;j++)
        {
            if(a[i][j]=='W')
            {
                f=1;
                break;
            }
        }
        if(!f)
        sum++;
        f=0;
    }
    if(sum==8)
    {
        cout<<sum;
        return 0;
    }
    for(int i=1;i<=8;i++)
    {
    for(int j=1;j<=8;j++)
    {
        if(a[j][i]=='W')
        {
            f=1;
            break;
        }
    }
        if(!f)
        sum++;
        f=0;
    }
    cout<<sum;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值