CodeForces 7A Kalevitch and Chess

博客介绍了一道关于如何用最少的笔划绘制出客户指定的黑白棋盘图案的问题。通过分析每行每列全黑的情况来确定最少的绘制次数。

1、http://codeforces.com/problemset/problem/7/A

2、题目:

A. Kalevitch and Chess
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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

Input

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

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.

Sample test(s)
Input
WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
Output
3
Input
WWWWWWWW
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
1
3、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int nr[10],nc[10];
int main()
{
    char a[10][10];
    for(int i=1;i<=8;i++)
    {
        for(int j=1;j<=8;j++)
        {
            scanf("%c",&a[i][j]);
        }
        getchar();
    }
    int r=0;
    int c=0;
    memset(nr,0,sizeof(nr));
    memset(nc,0,sizeof(nc));
    for(int i=1;i<=8;i++)
    {
        int flag=0;
        for(int j=1;j<=8;j++)
        {
            if(a[i][j]=='W')
            flag=1;
        }
        if(flag==0)
        {
            r++;
            nr[i]=1;
        }
    }
    for(int j=1;j<=8;j++)
    {
        int flag=0;
        for(int i=1;i<=8;i++)
        {
            if(a[i][j]=='W')
            flag=1;
        }
        if(flag==0)
        {
            nc[j]=1;
            c++;
        }
    }
    //printf("%d %d\n",r,c);
    if(r>c)
    {
        int cnt=0;
        for(int i=1;i<=8;i++)
        {
            if(nr[i]==1)
            {
                for(int j=1;j<=8;j++)
                a[i][j]='W';
            }
        }
        for(int i=1;i<=8;i++)
        {
            if(nc[i]==1)
            {
                for(int j=1;j<=8;j++)
                {
                    if(a[j][i]=='B')
                    {
                        cnt++;
                        break;
                    }
                }
            }
        }
        printf("%d\n",cnt+r);
    }
    else
    {
        int cnt=0;
        for(int j=1;j<=8;j++)
        {
            if(nc[j]==1)
            {
                for(int i=1;i<=8;i++)
                {
                    a[i][j]='W';

                }

            }
        }
        for(int i=1;i<=8;i++)
        {
            if(nr[i]==1)
            {
                for(int j=1;j<=8;j++)
                {
                    if(a[i][j]=='B')
                    {
                        cnt++;
                        break;
                    }
                }
            }
        }
        printf("%d\n",cnt+c);
    }
    return 0;
}
/*
BBBBBBBB
BWWWWWWW
BWWWWWWW
BWWWWWWW
BWWWWWWW
BWWWWWWW
BWWWWWWW
BWWWWWWW
*/


虽然给定引用中未直接提及“Kuroni and Simple Strings”题目的详细信息,但通常这类题目可能与字符串处理、括号匹配等相关。一般而言,题目可能会给出一个由括号组成的字符串,要求找出能移除的最大数量的不相交的合法括号对,并输出移除这些括号对后的相关信息。 ### 解法分析 #### 栈解法 栈解法是处理括号匹配问题的经典方法。通过遍历字符串,将左括号压入栈中,遇到右括号时,若栈顶为左括号,则将栈顶元素弹出,表示这是一对匹配的括号。 ```python s = input() stack = [] pairs = [] for i, char in enumerate(s): if char == '(': stack.append(i) else: if stack: left_index = stack.pop() pairs.append((left_index + 1, i + 1)) if not pairs: print(0) else: print(1) print(len(pairs) * 2) result = [] for l, r in pairs: result.extend([l, r]) result.sort() print(" ".join(map(str, result))) ``` #### 双指针解法 双指针解法从字符串的两端向中间遍历,分别使用两个指针 `left` 和 `right`。`left` 指针从左向右寻找 `(`,`right` 指针从右向左寻找 `)`,当找到一对匹配的括号时,将它们标记为已移除,继续寻找下一对匹配的括号,直到无法再找到匹配的括号为止。 ```python s = input() n = len(s) left = 0 right = n - 1 pairs = [] while left < right: while left < right and s[left] != '(': left += 1 while left < right and s[right] != ')': right -= 1 if left < right: pairs.append((left + 1, right + 1)) left += 1 right -= 1 if not pairs: print(0) else: print(1) print(len(pairs) * 2) result = [] for l, r in pairs: result.extend([l, r]) result.sort() print(" ".join(map(str, result))) ``` ### 复杂度分析 - **栈解法**:时间复杂度为 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度为 $O(n)$,主要用于栈的空间开销。 - **双指针解法**:时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,主要用于存储匹配的括号对。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值