J.This Ain’t Your Grandpa’s Checkerboard
题面:
You are given an n−by−n grid where each square is colored either black or white. A grid is correct if all of the following conditions are satisfied:
Every row has the same number of black squares as it has white squares.
Every column has the same number of black squares as it has white squares.
No row or column has 3 or more consecutive squares of the same color.
Given a grid, determine whether it is correct.
输入描述:
The first line contains an integer n(2≤n≤24; n is even ) . Each of the next n lines contains a string of length n consisting solely of the characters ‘B’ and ‘W’, representing the colors of the grid squares.
输出描述:
If the grid iscorrect, print the number 1 on a single line. Otherwise, print the number 0 on a single line.
中文题意:
行列黑白格子数量相等且行列中无连续三个同色格子。
求给定方阵是否满足。
代码:
#include<iostream>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
int main() {
int n;
char a[30][30];
bool flag = 1;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
int sum1, sum2;
for (int i = 0; i < n; i++) {
sum1 = sum2 = 0;
for (int j = 0; j < n; j++) {
if (a[i][j] == 'B') sum1++;
else sum2++;
}
if (sum1 != sum2) {
cout << 0 << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
sum1 = sum2 = 0;
for (int j = 0; j < n; j++) {
if (a[j][i] == 'B') sum1++;
else sum2++;
}
if (sum1 != sum2) {
cout << 0 << endl;
return 0;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 2; j++) {
if (a[i][j] == a[i][j + 1]&&a[i][j + 1] == a[i][j + 2]) {
flag = 0;
}
}
for (int j = 0; j < n; j++)
for (int i = 0; i < n - 2; i++) {
if (a[i][j] == a[i + 1][j] &&a[i + 1][j] == a[i + 2][j]) {
flag = 0;
}
}
if (!flag) cout << 0 << endl;
else cout << 1 << endl;
}
一开始 if (a[i][j] == a[i + 1][j] &&a[i + 1][j] == a[i + 2][j])
写成if (a[i][j] == a[i + 1][j] == a[i + 2][j])
我也是服了!