Codeforce_837B - Flag of Berland

本文介绍了一种用于验证是否符合Berland旗帜规格的算法。该算法通过检查输入矩阵是否由三种颜色组成且每种颜色各占据相等宽度的三条平行条纹来判断。文章详细解释了如何针对不同方向的条纹进行验证,并给出了具体的实现代码。

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

B. Flag of Berland
The flag of Berland is such rectangular field n × m that satisfies following conditions:

Flag consists of three colors which correspond to letters ‘R’, ‘G’ and ‘B’.
Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color.
Each color should be used in exactly one stripe.
You are given a field n × m, consisting of characters ‘R’, ‘G’ and ‘B’. Output “YES” (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print “NO” (without quotes).

Input
The first line contains two integer numbers n and m (1 ≤ n, m ≤ 100) — the sizes of the field.

Each of the following n lines consisting of m characters ‘R’, ‘G’ and ‘B’ — the description of the field.

Output
Print “YES” (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print “NO” (without quotes).

Examples
input
6 5
RRRRR
RRRRR
BBBBB
BBBBB
GGGGG
GGGGG
output
YES
input
4 3
BRG
BRG
BRG
BRG
output
YES
input
6 7
RRRGGGG
RRRGGGG
RRRGGGG
RRRBBBB
RRRBBBB
RRRBBBB
output
NO
input
4 4
RRRR
RRRR
BBBB
GGGG
output
NO
Note
The field in the third example doesn’t have three parralel stripes.

Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights — 2, 1 and 1.

wa了两天,终于a了,说实话我也表达不出,但是就是模拟解决问题

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
    int n,m,i,j;
    string str[111],str1[111];
    while(cin>>n>>m)
    {

        for(i=0; i<n+1; i++)
            str[i].clear();
        for(i=0; i<n; i++)
            cin>>str[i];
        /*   for(i=0;i<m;i++)
           {
               for(j=0;j<n;j++)
                   cout<<str[j][i];
               puts("");
           }*/
        if(n%3!=0&&m%3!=0)
        {
            puts("NO");

        }
        else
        {
            map<char,int> q;
            q.clear();
            int t1=0,t2=0;
            int s1=-1,s2=-1,s3=-1,ss1=-1,ss2=-1,ss3=-1;
            if(n%3==0)
            {
                s1=s2=s3=0;
                int t=n/3;
                t1=t*m;

                for(i=0; i<t; i++)
                {
                    for(j=0; j<m; j++)
                    {
                        if(str[i][j]==str[0][0]&&!q[str[0][0]])
                            s1+=1;
                    }
                }
                q[str[0][0]]=1;
                for(i=t; i<2*t; i++)
                {
                    for(j=0; j<m; j++)
                    {
                        if(str[i][j]==str[t][0]&&!q[str[t][0]])
                            s2+=2;
                    }
                }
                q[str[t][0]]=1;
                for(i=2*t; i<3*t; i++)
                {
                    for(j=0; j<m; j++)
                    {
                        if(str[i][j]==str[2*t][0]&&!q[str[2*t][0]])
                            s3+=3;
                    }
                }
                q[str[2*t][0]]=1;
            }
            if(m%3==0)
            {
                q.clear();
                ss1=ss2=ss3=0;
                int t=m/3;
                t2=t*n;
                for(i=0; i<t; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(str[j][i]==str[0][0]&&!q[str[0][0]])
                            ss1+=1;
                        //    cout<<str[j][i];
                    }
                    //  puts("");
                }
                q[str[0][0]]=1;
                for(i=t; i<2*t; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(str[j][i]==str[0][t]&&!q[str[0][t]])
                            ss2+=2;
                        //   cout<<str[j][i];
                    }
                    // puts("");
                }
                q[str[0][t]]=1;
                for(i=2*t; i<m; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(str[j][i]==str[0][2*t]&&!q[str[0][2*t]])
                            ss3+=3;
                    }
                }
                q[str[0][2*t]]=1;

            }
          //  cout<<s1<<" "<<s2<<" "<<s3<<endl;
            if(s1==t1&&s2==2*t1&&s3==3*t1)
            {
                // puts("1");
                puts("YES");
            }
            else if(ss1==t2&&ss2==2*t2&&ss3==3*t2)
            {
                // puts("2");

                  puts("YES");
            }
            else
            {
                //  puts("3");
                puts("NO");
            }

        }
        //  cout<<f<<" "<<ff<<endl;

    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值