3.7牛客2021年度训练联盟热身训练赛第一场J.[模拟]
链接:https://ac.nowcoder.com/acm/contest/12606/J
来源:牛客网
题目描述
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.
示例1
输入
复制
4 WBBW WBWB BWWB BWBW
输出
复制
1
示例2
输入
复制
4 BWWB BWBB WBBW WBWW
输出
复制
0
示例3
输入
复制
6 BWBWWB WBWBWB WBBWBW BBWBWW BWWBBW WWBWBB
输出
复制
0
示例4
输入
复制
6 WWBBWB BBWWBW WBWBWB BWBWBW BWBBWW WBWWBB
输出
复制
1
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 101000;
#define inf 0xfffffffchar ma[30][30];int col[30],row[30];int main()
{int n;cin>>n;bool is_correct=true;memset(col,0,sizeof(col));memset(row,0,sizeof(row));for(int i=0;i<n;i++){for(int j=0;j<n;j++){cin>>ma[i][j];if(ma[i][j]=='W'){row[i]++;col[j]++;}}}for(int i=0;i<n;i++){for(int j=0;j<n;j++){if((ma[i][j]==ma[i][j+1])&&(ma[i][j+1]==ma[i][j+2])){is_correct=false;//cout<<1<<endl;break;}}}for(int j=0;j<n;j++){for(int i=0;i<n;i++){if((ma[i][j]==ma[i+1][j])&&(ma[i+1][j]==ma[i+2][j])){is_correct=false;//cout<<2<<endl;break;}}}for(int i=0;i<n;i++){if(row[i]!=(n-row[i])){is_correct=false;break;}if(col[i]!=(n-col[i])){is_correct=false;//cout<<4<<endl;break;}}if(is_correct==true)cout<<1;elsecout<<0;return 0;
}