Codeforces Beta Round #7 A. Kalevitch and Chess

本文介绍了一种解决8x8矩阵中黑白块最少操作次数以恢复全白状态的方法。通过记录每行和每列黑块数量,算法能够高效地计算所需操作次数。代码展示了实现这一解决方案的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意

给出一个8*8的矩阵包含黑白色块,问最少操作几次可以恢复白色。

解题思路

水题,记录下行和列黑块的个数,进行操作即可。

题目代码

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <time.h>
#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
using namespace std;

int cntx[8],cnty[8];
char s[10][10];

int main()
{
    for(int i=0;i<8;i++)
    {
        scanf("%s",s[i]);
        for(int j=0;j<8;j++)
        {
            if(s[i][j]=='B')
            {
                cntx[i]++;
                cnty[j]++;
            }
        }
    }
    int ans=0;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(s[i][j]=='B')
            {
                if(cntx[i]==8)
                {
                    cntx[i]=0;
                    for(int k=0;k<8;k++)
                    {
                        s[i][k]='W';
                    }
                    ans++;
                    break;
                }
                else if(cnty[j]==8)
                {
                    cnty[j]=0;
                    for(int k=0;k<8;k++)
                    {
                        s[k][j]='W';
                    }
                    ans++;
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值