codeforce 837B Flag of Berland(矩形判断)
2017-08-08 17:49
260 查看
B. Flag of Berland
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
题目非常简单,就是判断一下这一面旗帜是不是有三个大小相同的矩形区域组成,也可以理解为有两条支线分割出完全相同的三部分,且颜色不同
题目很简单,但很容易翻车,准备的数据很少,但最后hack掉了一堆人。我用的就是判断这三个区域的大小,应为左上角和右下角肯定是不同的,就在边界找到与他们相同的,记录长度与宽带,最后要判断三个矩形区域面积和是否和大面积相等,然后要再判断下这个区域是否颜色完全相同(本人就这里。。没判断)
代码如下:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
题目非常简单,就是判断一下这一面旗帜是不是有三个大小相同的矩形区域组成,也可以理解为有两条支线分割出完全相同的三部分,且颜色不同
题目很简单,但很容易翻车,准备的数据很少,但最后hack掉了一堆人。我用的就是判断这三个区域的大小,应为左上角和右下角肯定是不同的,就在边界找到与他们相同的,记录长度与宽带,最后要判断三个矩形区域面积和是否和大面积相等,然后要再判断下这个区域是否颜色完全相同(本人就这里。。没判断)
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int MAX = 110; char Map[MAX][MAX]; bool book[300]; int n,m; bool solve(){ memset(book,0,sizeof(book)); // if((n < 3) && (m < 3)) return false; if((n % 3!= 0)&&(m % 3!=0)) return false; int w1 = 0,h1 = 0; book[Map[0][0]] = true; for(int i=0;i<n;i++) if(Map[i][0] == Map[0][0]) h1++; else break; for(int i=0;i<m;i++) if(Map[0][i] == Map[0][0]) w1++; else break; for(int i=0;i<h1;i++) for(int j=0;j<w1;j++) if(Map[i][j] != Map[0][0]) return false; int w2 = 0,h2 = 0; for(int i=n-1;i>=0;i--) if(Map[i][m-1] == Map[n-1][m-1] && !book[Map[i][m-1]] ) h2++; else break; for(int i=m-1;i>=0;i--) if(Map[n-1][i] == Map[n-1][m-1] && !book[Map[n-1][i]]) w2++; else break; for(int i=n-1;i>=(n-h2);i--){ for(int j=m-1;j>=(m-w2);j--) if(Map[i][j] != Map[n-1][m-1]) return false; } book[Map[n-1][m-1]] = true; int x = 0,y = 0; for(int i=0;i<n;i++){ bool isok = false; for(int j=0;j<m;j++){ if(!(i<h1&&j<w1)){ x = i;y = j; isok = true; break; } } if(isok) break; } int w3 = 0,h3 = 0; for(int i=y;i<m;i++) if(Map[x][i] == Map[x][y] && !book[Map[x][i]]) w3++; else break; for(int i=x;i<n;i++) if(Map[i][y] == Map[x][y] && !book[Map[i][y]]) h3++; else break; for(int i=x;i<x+h3;i++) for(int j=y;j<y+w3;j++) if(Map[i][j] != Map[x][y]) return false; // printf("%d,%d,%d,%d,%d\n",w1,h1,w2,h2,w3,h3); if((h1 == h2 && h2 == h3 && w1 == w2 && w2 == w3) &&(h1*w1+h2*w2+h3*w3 == n*m)) return true; return false; } int main(void){ // freopen("1.txt","r",stdin); scanf("%d %d",&n,&m); for(int i=0;i<n;i++) scanf("%s",Map[i]); if(solve()) printf("YES\n"); else printf("NO\n"); return 0; }
相关文章推荐
- Codeforce_837B - Flag of Berland
- 矩形判断碰撞
- 矩形重叠判断
- codeforce 608C Chain Reaction
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- 平面矩形与圆相交(重叠)的判断 [转]
- Code Fragment-对于if语句中的boolean值分开判断的处理。
- 【求指导】判断符合要求的四个点围成的是否为矩形
- c#判断点是否在矩形上
- Codeforce - 204B - Little Elephant and Cards
- 四点判断能否围成矩形
- 判断点是否在三角形内 和 点是否在矩形内
- 素数判断及矩形交集的实现
- 如何判断两个矩形相交
- codeforce 67D - Optical Experiment(dp)
- 判断两个矩形相交以及求出相交的区域
- 判断两个矩形是否相交的原理详解
- codeforce Vladik and Complicated Book
- [zz]判断两矩形是否交叉(包括包含的情况)
- code force 401B. Game of Credit Cards