【CodeForces】837B - Flag of Berland

本文介绍了一个用于判断由三种颜色组成的旗帜图案是否相同的算法。通过分析旗帜的横纵比,检查颜色分布的一致性来实现这一目标。算法首先验证旗帜是否可以被均匀划分为三等份,并确保每种颜色出现的次数相等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/problemset/problem/837/B

一面旗帜上有3种颜色,判断三种颜色构成的图形形状是否相同。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;

int n,m;
char g[105][105];
int cnt[300]={0};
bool flag,ok;


int main(){

    cin >> n >> m;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            cin >> g[i][j];
        }
    }

    if (n%3==0){
        flag=true;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (g[i][j]!=g[i/(n/3)*(n/3)][0]){
                    flag=false;
                }
                cnt[g[i][j]]++;
            }
        }
        if (flag){
            ok=true;
        }
    }

    if (m%3==0){
        flag=true;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (g[i][j]!=g[0][(j/(m/3))*(m/3)]){
                    flag=false;
                }
                cnt[g[i][j]]++;
            }
        }
        if (flag){
            ok=true;
        }
    }

    if (ok&&(cnt['R']==cnt['G']&&cnt['R']==cnt['B'])){
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值