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).
InputThe 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.
OutputPrint "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 NoteThe 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.
分析:三个字母R,G,B的排列,代表三面旗帜,所以相同字母肯定要在一起,如果是按行排的话,则要每一排都要
是相同的字母,如果是按列排的话,每一列都要是相同的字母,如果满足条件就输出YES,否则输出NO,本题
要考虑三个点,第一,三种字母的数量相等,第二,如果是按行排,则每一行的字母要相同,如果是竖着排,则
每列的字母要相同。第三,相同的字母应排在一起,如果这一点不好理解,下面来看一组测试数据:
6 5
RRRRR
GGGGG
BBBBB
BBBBB
GGGGG
RRRRR
这组数据答案应该是NO。
#include <iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { int n,m,bj1,bj2,ee; int h[3]={0}; scanf("%d %d",&n,&m); char s[n][m]; getchar(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&s[i][j]); if(s[i][j]=='R') { h[0]++; } if(s[i][j]=='G') { h[1]++; } if(s[i][j]=='B') { h[2]++; } } getchar(); } bj1=0; bj2=0; for(int i=0;i<n;i++) { for(int j=0;j<m-1;j++) { if(s[i][j]!=s[i][j+1]) { bj1=1; } } } for(int i=0;i<m;i++) { for(int j=0;j<n-1;j++) { if(s[j][i]!=s[j+1][i]) { bj2=2; } } } if(h[0]==h[1]&&h[0]==h[2]) { if(bj1==0&&n%3==0) { ee=0; for(int i=0;i<n-1;i++) { if(s[i][0]!=s[i+1][0]) { ee+=1; } } if(ee==2) printf("YES\n"); else printf("NO\n"); } else if(bj2==0&&m%3==0) { ee=0; for(int i=0;i<m-1;i++) { if(s[0][i]!=s[0][i+1]) { ee+=1; } } if(ee==2) printf("YES\n"); else printf("NO\n"); } else printf("NO\n"); } else printf("NO\n"); return 0; }
