题意是说,要把一个8X8的棋盘的黑色(B)的都刷白(W),一刷就一行或者一列,问最少刷多少次
这个的话我们可以先遍历一行全是黑的的有多少行,然后再看列需要刷多少行
官方标签:暴力,构造算法
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[8][8];
int sum=0;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cin>>s[i][j];
}
}
int sum2=0;
int sum3=0;
for(int i=0;i<8;i++)
{
int sum1=0;
for(int j=0;j<8;j++)
{
if(s[i][j]=='B')
sum1++;
}
if(sum1==8)
sum2++;
else
sum3=sum1; //优化关键:横刷刷完了之后看看剩下点啥全是纵刷 虽然个人觉得为了不被Hack要不要加一个 (if sum1>sum3) sum3=sum1
}
cout<<sum2+sum3<<endl;
return 0;
}
Python:
# input
matrix = []
for i in range(8):
matrix.append(raw_input())
# getAns
ans = 0
# row
for i in range(8):
if matrix[i][0] == 'B':
for j in range(8):
if matrix[i][j] == 'W':
break
if j == 7 and matrix[i][j] == 'B':
ans += 1
# col
for i in range(8):
if matrix[0][i] == 'B':
for j in range(8):
if matrix[j][i] == 'W':
break
if j == 7 and matrix[j][i] == 'B':
ans += 1
if ans == 16:
ans = 8
print ans