HDU 5319 Painter(模拟)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 

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

题意
题意大致就是在画板上画斜线,’\’方向的颜色是R,’/’方向的颜色是B,交叉点的颜色是G,要求每次画都要是连续的一段.问至少需要几笔才能画出来.

解析
有两种做法:
1.显而易见,对于第一行,每一个有颜色的点,则其必定是对应线段的一个端点(特指上端点).
基于这一点,我们可以从第一行开始处理,当其有颜色时,将该颜色代表的方向上的连续的点颜色消去,即如果当前点为’R’,则从当前的出发向右下角找颜色为’R’和’G’的连续点,将’R’变为’.’,将’G’变为’B’.如果当前点的颜色是’G’,则左下和右下各处理一次.
处理完一行后,第一行将全为’.’,则此时第二行又可以满足”每一个颜色的点必定是对应线段的一个端点”.逐行处理,消去所有线条以使得画板回到空白状态,处理的端点的个数即为所求的最小次数.
2.同样是找端点,但更为简洁也更快(实际上是赛后从其他人的博客里看来的).
如果当前点为’R’,若其左上角的点颜色为’R’或’G’,则当前点为线段的中间部分,否则为线段的端点.对于颜色为’B’或’G’的点同理,只需要看当前点在对应方向上的前一个点是否包含当前点的颜色就能确定其是否是(上)端点.
遍历所有点,统计端点数即可得出结果.

第一种做法

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <vector>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 #define MAXN    50+10
11 
12 char _map[MAXN][MAXN];
13    int N;
14 int len;
15 void Left(int x,int y)
16 {
17     for (int i=x,j=y;i<N&&j<len&&j>=0;i++,j--)
18         if (_map[i][j]=='B')
19         {
20             _map[i][j] = '.';
21         }
22         else if (_map[i][j] == 'G')
23         {
24             _map[i][j]='R';
25         }
26         else
27             break;
28 }
29 
30 void Right(int x,int y)
31 {
32     for (int i=x,j=y;i<N&&j<len&&j>=0;i++,j++)
33         if (_map[i][j]=='R')
34         {
35             _map[i][j] = '.';
36         }
37         else if (_map[i][j] == 'G')
38         {
39             _map[i][j]='B';
40         }
41         else
42             break;
43 }
44 
45 int main()
46 {
47    int T;
48    scanf("%d",&T);
49    while (T--)
50    {
51         scanf("%d",&N);
52         for (int i=0;i<N;i++)
53         {
54             scanf("%s",_map[i]);
55         }
56         int cnt=0;
57         len=strlen(_map[0]);
58         for (int i=0;i<N;i++)
59         {
60             for (int j=0;j<len;j++)
61             {
62                 if (_map[i][j]!='.')
63                 {
64                     if (_map[i][j]=='R')
65                     {
66                         cnt++;
67                         Right(i,j);
68                     }
69                     else if (_map[i][j]=='B')
70                     {
71                         cnt++;
72                         Left(i,j);
73                     }
74                     else if (_map[i][j]=='G')
75                     {
76                         cnt+=2;
77                         Left(i,j);
78                         Right(i,j);
79                     }
80                 }
81             }
82         }
83         printf("%d\n",cnt );
84    }
85     return 0;
86 }

 

 

posted @ 2018-06-18 10:34  hemeiwolong  阅读(166)  评论(0编辑  收藏  举报