NC218995. ThisAin'tYourGrandpa'sCheckerboard
描述
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
C(clang11) 解法, 执行用时: 2ms, 内存消耗: 376K, 提交时间: 2021-03-07 12:52:25
#include<stdio.h> int main(){ int n,i,j,t,answer,k; char a[30][30]; answer=1; scanf("%d",&n); for (i=1;i<=n;i++){ k=0; scanf("\n"); for (j=1;j<=n;j++){ scanf("%c",&a[i][j]); if (a[i][j]=='B') k++; } if (i==1) t=k; if (k!=t){ answer=0; } } for (i=3;i<=n;i++){ for (j=1;j<=n;j++){ if (a[i][j]==a[i-1][j]&&a[i-1][j]==a[i-2][j]){ answer=0; } } } for (i=1;i<=n;i++){ for (j=3;j<=n;j++){ if (a[i][j]==a[i][j-1]&&a[i][j-1]==a[i][j-2]){ answer=0; } } } printf("%d",answer); return 0; }
C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 380K, 提交时间: 2021-03-07 14:55:48
#include<bits/stdc++.h> using namespace std; int n; char a[30][30]; int check(int x,int y){ int bs=0; int hs=0; for(int i=0;i<n;i++){ if(a[x][i]=='B'){ bs++; } if(a[i][y]=='B'){ hs++; } } if(hs!=n/2||bs!=n/2){ return 1; } else{ return 0; } } int main(){ cin>>n; for(int i=0;i<n;i++){ scanf("%s",&a[i]); } int p=0; for(int i=0;i<n;i++){ p=check(i,i); if(p==1){ break; } } if(p==0){ cout<<"1"<<endl; } else{ cout<<"0"<<endl; } return 0; }
pypy3(pypy3.6.1) 解法, 执行用时: 55ms, 内存消耗: 19600K, 提交时间: 2021-03-08 23:27:43
n = int(input()) grid = [ input() for i in range(n) ] def fai(): print('0') exit(0) for s in grid: if s.count('W') != s.count('B'): fai() if s.find('WWW') != -1 or s.find('BBB') != -1: fai() for s in zip(*grid): t = ''.join(s) if t.count('W') != t.count('B'): fai() if t.find('WWW') != -1 or t.find('BBB') != -1: fai() print('1') exit(0)
Python3(3.9) 解法, 执行用时: 16ms, 内存消耗: 2816K, 提交时间: 2021-03-07 17:19:15
#!/usr/bin/env python3 n = int(input()) board = [input() for _ in range(n)] valid = True for _ in range(2): valid = valid and all('BBB' not in row and 'WWW' not in row and row.count('B') == row.count('W') for row in board) board = [''.join(row) for row in zip(*board)] print(1 if valid else 0)