Painter

来源:互联网 发布:如何优化搜索引擎 编辑:程序博客网 时间:2024/05/16 05:51

Painter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2221    Accepted Submission(s): 885


Problem Description
Mr. Hdu is an painter, as we all know, painters need ideas to innovate , one day, he got stuck in rut and the ideas dry up, he took out a drawing board and began to draw casually. Imagine the board is a rectangle, consists of several square grids. He drew diagonally, so there are two kinds of draws, one is like ‘\’ , the other is like ‘/’. In each draw he choose arbitrary number of grids to draw. He always drew the first kind in red color, and drew the other kind in blue color, when a grid is drew by both red and blue, it becomes green. A grid will never be drew by the same color more than one time. Now give you the ultimate state of the board, can you calculate the minimum time of draws to reach this state.
 

Input
The first line is an integer T describe the number of test cases.
Each test case begins with an integer number n describe the number of rows of the drawing board.
Then n lines of string consist of ‘R’ ‘B’ ‘G’ and ‘.’ of the same length. ‘.’ means the grid has not been drawn.
1<=n<=50
The number of column of the rectangle is also less than 50.
Output
Output an integer as described in the problem description.
 

Output
Output an integer as described in the problem description.
 

Sample Input
24RR.B.RG..BRRB..R4RRBBRGGBBGGRBBRR
 

Sample Output
36
 

Author
ZSTU
 

Source
2015 Multi-University Training Contest 3

题目也是看了几遍看懂,大意:红色 ‘\’ 方向涂色,蓝色 '/'方向涂色,红蓝交差成绿色,还有一个地方要仔细“Imagine the board is a rectangle,”输入是可以为矩形的,之前没看仔细,看的案例一直以为是正方形导致了多次WA。

代码如下:
#include<cstdio>#include<cstring>#include<cctype>#include<algorithm>#include<set>#include<cstring>#include<string>#include<iostream>using namespace std;char a[100][100];int n;void f(int i,int j,char c){    /*if(i>=n||j>=n||i<0||j<0)        return ;*///这里之前误以为正方形  从而导致结束条件的错误    if(a[i][j]=='.')        return ;        if(a[i][j]!=c&&a[i][j]!='G') return ;    if(a[i][j]==c)    a[i][j]='.';        else            if(a[i][j]=='G')        {            if(c=='R')                a[i][j]='B';            else                if(c=='B')                a[i][j]='R';        }        if(c=='R')            f(i+1,j+1,c);        else if(c=='B')            f(i+1,j-1,c);}int main (){    int t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);       // getchar();        memset(a,0,sizeof(a));        for(int i=0;i<n;i++){            scanf("%s",a[i]);//用%s输入一个字符串,之前以为是正方形,于是用了两个for循环 %c一个个输。        }        int n1=strlen(a[0]);//取矩形的宽度        int ans=0;         for(int i=0;i<n;i++)        for(int j=0;j<n1;j++){                if(a[i][j]=='R')                {                    ans++;                    f(i,j,'R');                }                else                    if(a[i][j]=='B')                {                    ans++;                    f(i,j,'B');                }                else                     if(a[i][j]=='G')                {                    ans+=2;                    f(i,j,'R');                    f(i,j,'B');                }        }        printf("%d\n",ans);    }}



原创粉丝点击