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:
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 |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
只有在同一对角线的各自会影响到对方,所以按照对角线的方式来遍历每个格子,然后记录每个格子的开始和结束位置,之后做一下处理
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 };
来源:https://www.cnblogs.com/chkkch/archive/2012/12/02/2798173.html