codeforce 837B Flag of Berland(矩形判断)

来源:互联网 发布:协同工作软件 编辑:程序博客网 时间:2024/06/11 21:22

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掉了一堆人。我用的就是判断这三个区域的大小,应为左上角和右下角肯定是不同的,就在边界找到与他们相同的,记录长度与宽带,最后要判断三个矩形区域面积和是否和大面积相等,然后要再判断下这个区域是否颜色完全相同(本人就这里。。没判断)

代码如下:

#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;}