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).
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.
Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes).
6 5
RRRRR
RRRRR
BBBBB
BBBBB
GGGGG
GGGGG
YES
4 3
BRG
BRG
BRG
BRG
YES
6 7
RRRGGGG
RRRGGGG
RRRGGGG
RRRBBBB
RRRBBBB
RRRBBBB
NO
4 4
RRRR
RRRR
BBBB
GGGG
NO
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.
这题理解了就好做,就是条件有点复杂,需要好几次判断。(也可能是我自己想多了)
下面的代码其实可以合并条件,代码量可以缩小一半,但是我不愿改了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 using namespace std; 6 char s[105][105]; 7 set<char> st; 8 int main(){ 9 int n,m; 10 cin>>n>>m; 11 bool prime=true; 12 for(int i=1;i<=n;i++){ 13 for(int j=1;j<=m;j++){ 14 cin>>s[i][j]; 15 } 16 } 17 if(n%3!=0&&m%3!=0){ 18 prime= false; 19 }else if(n%3==0&&m%3!=0){ 20 char a=s[1][1]; 21 for(int i=1;i<=n/3;i++) { 22 for(int j=1;j<=m;j++){ 23 if(a!=s[i][j]){ 24 prime=false; 25 break; 26 } 27 }if(!prime) break; 28 }st.insert(a); 29 char b=s[n/3+1][1]; 30 for(int i=n/3+1;i<=2*n/3;i++){ 31 for(int j=1;j<=m;j++){ 32 if(b!=s[i][j]){ 33 prime=false; 34 break; 35 } 36 }if(!prime) break; 37 }st.insert(b); 38 char c=s[n][1]; 39 for(int i=2*n/3+1;i<=n;i++){ 40 for(int j=1;j<=m;j++){ 41 if(c!=s[i][j]){ 42 prime=false; 43 break; 44 } 45 }if(!prime) break; 46 }st.insert(c); 47 }else if(n%3!=0&&m%3==0){ 48 char a=s[1][1]; 49 for(int i=1;i<=n;i++) { 50 for(int j=1;j<=m/3;j++){ 51 if(a!=s[i][j]){ 52 prime=false; 53 break; 54 } 55 }if(!prime) break; 56 }st.insert(a); 57 char b=s[1][m/3+1]; 58 for(int i=1;i<=n;i++){ 59 for(int j=m/3+1;j<=2*m/3;j++){ 60 if(b!=s[i][j]){ 61 prime=false; 62 break; 63 } 64 }if(!prime) break; 65 }st.insert(b); 66 char c=s[1][m]; 67 for(int i=1;i<=n;i++){ 68 for(int j=2*m/3+1;j<=m;j++){ 69 if(c!=s[i][j]){ 70 prime=false; 71 break; 72 } 73 }if(!prime) break; 74 }st.insert(c); 75 }else{ 76 if(s[1][1]==s[1][m]){ 77 char a=s[1][1]; 78 for(int i=1;i<=n/3;i++) { 79 for(int j=1;j<=m;j++){ 80 if(a!=s[i][j]){ 81 prime=false; 82 break; 83 } 84 }if(!prime) break; 85 }st.insert(a); 86 char b=s[n/3+1][1]; 87 for(int i=n/3+1;i<=2*n/3;i++){ 88 for(int j=1;j<=m;j++){ 89 if(b!=s[i][j]){ 90 prime=false; 91 break; 92 } 93 }if(!prime) break; 94 }st.insert(b); 95 char c=s[n][1]; 96 for(int i=2*n/3+1;i<=n;i++){ 97 for(int j=1;j<=m;j++){ 98 if(c!=s[i][j]){ 99 prime=false; 100 break; 101 } 102 }if(!prime) break; 103 }st.insert(c); 104 }else{ 105 char a=s[1][1]; 106 for(int i=1;i<=n;i++) { 107 for(int j=1;j<=m/3;j++){ 108 if(a!=s[i][j]){ 109 prime=false; 110 break; 111 } 112 }if(!prime) break; 113 }st.insert(a); 114 char b=s[1][m/3+1]; 115 for(int i=1;i<=n;i++){ 116 for(int j=m/3+1;j<=2*m/3;j++){ 117 if(b!=s[i][j]){ 118 prime=false; 119 break; 120 } 121 }if(!prime) break; 122 }st.insert(b); 123 char c=s[1][m]; 124 for(int i=1;i<=n;i++){ 125 for(int j=2*m/3+1;j<=m;j++){ 126 if(c!=s[i][j]){ 127 prime=false; 128 break; 129 } 130 }if(!prime) break; 131 }st.insert(c); 132 } 133 } 134 if(!prime) 135 cout<<"NO"<<endl; 136 else{ 137 if(st.count('R')&&st.count('B')&&st.count('G')) 138 cout<<"YES"<<endl; 139 else 140 cout<<"NO"<<endl; 141 } 142 return 0; 143 }