2021年度牛客训练联盟 J.This Ain‘t Your Grandpa‘s Checkerboard

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])

我也是服了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值