Painter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1519 Accepted Submission(s): 636
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.
2 4 RR.B .RG. .BRR B..R 4 RRBB RGGB BGGR BBRR
3 6
题意:
这里我们有两把刷子,/这么刷的只能是B,\这么刷的只能是R,G是B R的混合颜色,起点不一定是左上角,终点也不一定是右下角。
这里我们采用逆向思维:找起点。
我们既然确定不了终点,那我们可以确定起点,既然我们从白纸画成样例很难,我们就逆向思维考虑问题。
那么,怎么算是个起点呢?
我们知道,我们这里的画笔是45°画的,所以我们这里判断是否为起点并不难:如果当前点不是点(空白),如果当前点是B,那就向右上方找一个格子,如果那个格子也是B的话,说明这个点不是起点,如果那个点不是B也不是G,那说明这个点是起点。所以我们这里能对应写出核心代码:
if(a[i][j]=='R'||a[i][j]=='G')//如果当前点被R画过。
{
if(a[i-1][j-1]!='R'&&a[i-1][j-1]!='G')//并且他的左上角格子没有被R画过,说明这个点是这条R划线的起点。
output++;
}
if(a[i][j]=='B'||a[i][j]=='G')//同上例。
{
if(a[i-1][j+1]!='B'&&a[i-1][j+1]!='G')
output++;
}
然后我们要遍历所有点,为了避免边界点会有非法访问的问题。我们这里输入的时候是这样的:
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
}
int m=strlen(a[1]+1);//注意题干中说的是矩形。
然后上完整的AC代码:
#include<stdio.h>
#include<string.h>
char a[55][55];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int output=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
}
int m=strlen(a[1]+1);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='R'||a[i][j]=='G')
{
if(a[i-1][j-1]!='R'&&a[i-1][j-1]!='G')
output++;
}
if(a[i][j]=='B'||a[i][j]=='G')
{
if(a[i-1][j+1]!='B'&&a[i-1][j+1]!='G')
output++;
}
}
}
printf("%d\n",output);
}
}