[TopCoder][SRM] SRM 562 DIV 2

Problem Statement

    

Cucumber Boy likes drawing pictures. Today, he plans to draw a picture using a very simple graphics editor.

 

The editor has the following functions:

  • The canvas is an infinite two-dimensional grid of pixels.
  • There are only two colors: black, and transparent. These are denoted 'B' and '.' (a period), respectively.
  • The editor has a clipboard that contains a rectangular picture.
  • The editor can take the picture in the clipboard and paste it onto any corresponding rectangle of the canvas. The user just has to select the pixel of the canvas where the upper left corner of the clipboard will be pasted.
  • When pasting the picture, the black pixels of the picture in the clipboard will overwrite their corresponding pixels on the canvas. The pixels that are transparent in the clipboard picture do not change the canvas.

 

At this moment, all pixels on the infinite canvas are transparent. Cucumber Boy has already stored a picture in the clipboard. You are given this picture as a vector <string> clipboard.

 

Cucumber Boy now wants to paste the clipboard picture onto the canvas exactly T times in a row.

For each i, when pasting the clipboard for the i-th time, he will choose the pixel (i,i) as the upper left corner of the pasted picture.

 

You are given the vector <string> clipboard and the int T. Return the number of black pixels on the canvas after all the pasting is finished.

Definition

    
Class: PastingPaintingDivTwo
Method: countColors
Parameters: vector <string>, int
Returns: long long
Method signature: long long countColors(vector <string> clipboard, int T)
(be sure your method is public)
    
 

Constraints

- clipboard will contain between 1 and 50 elements, inclusive.
- Each element of clipboard will contain between 1 and 50 characters, inclusive.
- Each element of clipboard will contain the same number of characters.
- Each character of each element of clipboard will be 'B' or '.'.
- T will be between 1 and 1,000,000,000, inclusive.

Examples

0)  
    
{
"..B",
"B..",
"BB."
}
3
Returns: 10

1)  
    
{
"B...",
"....",
"....",
"...B"
}
2
Returns: 4
 
2)  
    
{"BBB"}
10000
Returns: 30000
 
3)  
    
{"."}
1000000000
Returns: 0
 
4)  
    
{
"BB.",
".B."
}
100
Returns: 201
 
5)  
    
{
"..........B..........",
".........B.B.........",
"........B...B........",
".......B.....B.......",
"......B..B.B..B......",
".....B...B.B...B.....",
"....B...........B....",
"...B...B.....B...B...",
"..B.....BBBBBB....B..",
".B..........BB.....B.",
"BBBBBBBBBBBBBBBBBBBBB"
}
1000000000
Returns: 21000000071

Note that the answer may overflow a 32-bit integer variable.

 

This is the image of clipboard in this example.

只有在同一对角线的各自会影响到对方,所以按照对角线的方式来遍历每个格子,然后记录每个格子的开始和结束位置,之后做一下处理

 1 #include <vector>
 2 #include <string>
 3 using namespace std;
 4 
 5 class PastingPaintingDivTwo
 6 {
 7     public:
 8         long long calc(vector<string> clipboard, int T, int x, int y)
 9         {
10             long long sum = 0;
11             int endX = -1;
12             int endY = -1;
13             while(x < clipboard.size() && y < clipboard[0].size())
14             {
15                 if (clipboard[x][y] == 'B')
16                 {
17                     int endCurX = x + T - 1;
18                     int endCurY = y + T - 1;
19                     int startCurX = max(x, endX);
20                     int startCurY = max(y, endY);
21 
22                     sum += max(0, endCurX - startCurX + 1);
23 
24                     endX = max(endCurX + 1, endX);
25                     endY = max(endCurY + 1, endY);
26                 }
27 
28                 x++;
29                 y++;
30             }
31 
32             return sum;
33         }
34 
35         long long countColors(vector<string> clipboard, int T)
36         {
37             if (clipboard.size() == 0)
38                 return 0;
39 
40             long long sum = 0;
41             for(int i = 0; i < clipboard.size(); i++)
42             {
43                 sum += calc(clipboard, T, i, 0);
44             }
45 
46             for(int i = 1; i < clipboard[0].size(); i++)
47             {
48                 sum += calc(clipboard, T, 0, i);
49             }
50 
51             return sum;
52         }
53 };

 

posted @ 2012-12-02 14:45  chkkch  阅读(688)  评论(0编辑  收藏  举报