[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
您将获得一个 n × n n×n n×n 的网格,网格中每个正方形的颜色为黑色或白色。如果满足以下所有条件,则网格是正确的:
* 每行的黑色方块数与白色方块数相同。
* 每列的黑色正方形数与白色方块数相同。
* 没有行或列具有 3 3 3 个及以上相同颜色的连续正方形。
给定网格,确定它是否正确。
<>输入格式
第一行一个数字 n n n( 2 ≤ n ≤ 24 2≤n≤24 2≤n≤24), 并且数字 n n n 是偶数。
接下来 n n n 行,每行包含一个长度为 n n n的由字符B和W组成的字符串,代表网格正方形的颜色。
<>输出格式
如果网格正确,请打印数字 1 在一行上。否则,请打印数字 0 在一行上。
<>样例输入
4 WBBW WBWB BWWB BWBW
<>样例输出
1
<>解题思路
关键在于状态的维护
采用三个参数来维护状态:
连续色块颜色,色块连续数,累计黑块数量
第三个参数很好理解,这里只说明前两个参数的使用
读入一个色块,有一下两种情况
(1)与当前连续色块颜色一致:色块连续数++
(2)与当前连续色块颜色不一致:修改当前连续色块颜色,初始化色块连续数为 1 1 1
AC代码如下
#include <iostream> #include <string> using namespace std; const int max_n = 24
; int n; int row_sum, row_seq, row_status = -1; int col_sum[max_n], col_seq[
max_n], col_status[max_n]; int main() { for (int i = 0; i < max_n; i++)
col_status[i] = -1; cin >> n; cin.ignore(); string str; bool ans = true, c; for
(int i = 0; i < n; i++) { getline(cin, str); int len = str.size(); if (ans) {
for (int j = 0; j < len; j++) { if (str[j] == 'B') c = 1; else c = 0; row_sum +=
c; if (row_status == c) { row_seq++; if (row_seq == 3) { ans = false; break; }
} else { row_seq = 1; row_status = c; } col_sum[j] += c; if (col_status[j] == c)
{ col_seq[j]++; if (col_seq[j] == 3) { ans = false; break; } } else { col_seq[j]
= 1; col_status[j] = c; } } if (row_sum != n / 2) ans = false; row_sum = 0;
row_seq= 0; row_status = -1; } } if (ans) { for (int i = 0; i < n; i++) if (
col_sum[i] != n / 2) { ans = false; break; } } cout << ans << endl; return 0; }