hdu 5319 Painter(模拟)(思维)(模拟)

本文探讨了在给定绘画板尺寸和特定颜色限制条件下的最短绘制时间问题,通过逆向思维找到起始点,实现从空白状态到最终图案的最优化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Painter

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


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
2 4 RR.B .RG. .BRR B..R 4 RRBB RGGB BGGR BBRR
 


Sample Output
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);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值