Painter

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


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
 
题意:给你n个长度相等的字符串,包含'R' 红色,'B' 蓝色 ,'G' 绿色,'.' 没有颜色,两种方式,'\' 斜向右下刷成红色, ’/'斜向左下刷成蓝色, 如果如果一个点被红色刷过又被蓝色           刷过,那么它就是绿色'G', 给你一面墙的最终形态,问你最少要刷多少次。
 思路:dfs爆搜就行了。
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<limits.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<complex>
 7 #include<cstring>
 8 #include<iomanip>
 9 #include<stdio.h>
10 #include<bitset>
11 #include<cctype>
12 #include<math.h>
13 #include<string>
14 #include<time.h>
15 #include<vector>
16 #include<cmath>
17 #include<queue>
18 #include<stack>
19 #include<list>
20 #include<map>
21 #include<set>
22 
23 #define LL long long
24 
25 using namespace std;
26 const LL mod = 1e9 + 7;
27 const double PI = acos(-1.0);
28 const double E = exp(1.0);
29 const int M = 1e2 + 5;
30 
31 string s[55];
32 bool red[55][55];
33 bool blue[55][55];
34 int sum;
35 int len;
36 int n;
37 
38 void dfs(int x, int y, int ok)
39 {
40     if(x <= 0 || x > n || y <= 0 || y > len)
41         return ;
42     if(ok == 1){
43         if(red[x][y]){
44             red[x][y] = 0;
45             dfs(x + 1, y + 1, 1);
46         }
47         else
48             return ;
49     }
50     else if(ok == 2){
51         if(blue[x][y]){
52             blue[x][y] = 0;
53             dfs(x + 1, y - 1, 2);
54         }
55         else
56             return ;
57     }
58     return ;
59 }
60 
61 int main()
62 {
63     //freopen("in.txt", "r", stdin);
64     //freopen("out.txt", "w", stdout);
65     int t;
66     cin >> t;
67     while( t-- ){
68         scanf("%d", &n);
69         for(int i = 1; i <= n; ++i)
70             cin >> s[i];
71         memset(red, 0, sizeof(red));
72         memset(blue, 0, sizeof(blue));
73         len = s[1].size();
74         sum = 0;
75         for(int i = 1; i <= n; ++i){
76             for(int j = 0; j < len; ++j){
77                 if(s[i][j] == 'R' || s[i][j] == 'G')
78                     red[i][j + 1] = 1;
79                 if(s[i][j] == 'B' || s[i][j] == 'G')
80                     blue[i][j + 1] = 1;
81             }
82         }
83         for(int i = 1; i <= n; ++i){
84             for(int j = 0; j < len; ++j){
85                 if(red[i][j + 1]){
86                     sum++;
87                     dfs(i, j + 1, 1);
88                 }
89                 if(blue[i][j + 1]){
90                     sum++;
91                     dfs(i, j + 1, 2);
92                 }
93             }
94         }
95         cout << sum << endl;
96     }
97     return 0;
98 }

 

posted on 2015-07-29 12:32  Unico  阅读(112)  评论(0)    收藏  举报