A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.
Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors. As a result, the quadtree need not be of uniform depth.
A modern computer artist works with black-and-white images of 32 × 32 units, for a total of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.
This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are black in the image, which is the result of adding the two images together.
In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.
Input
The first line of input specifies the number of test cases (N) your program has to process.
The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter ‘p’ indicates a parent node, the letter ‘f’ (full) a black quadrant and the letter ‘e’ (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).
Output
For each test case, print on one line the text ‘There are X black pixels.’, where X is the number of black pixels in the resulting image.
Sample Input
3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe
Sample Output
There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.
题目类型:二叉树递归遍历
算法分析:按照先序遍历的顺序构建,当遇到叶子节点'f'时,直接将其所包含的像素中仍没有计算过的累加起来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
/************************************************** filename :d.cpp author :maksyuki created time :2018/5/30 16:27:56 last modified :2018/5/30 16:59:27 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 32 + 6; bool color[maxn][maxn]; int ans; string s; void draw(int &pos, int x, int y, int h) { if(!s[pos]) return; char ch = s[pos++]; if(ch == 'p') { draw(pos, x, y + h / 2, h / 2); draw(pos, x, y, h / 2); draw(pos, x + h / 2, y, h / 2); draw(pos, x + h / 2, y + h / 2, h / 2); } else if(ch == 'f') { for(int i = x; i < x + h; i++) for(int j = y; j < y + h; j++) if(!color[i][j]) { color[i][j] = true; ans++; } } } int main() { #ifdef LOCAL CFF; //CFO; #endif int t; cin >> t; while(t--) { memset(color, false, sizeof(color)); ans = 0; for(int i = 1; i <= 2; i++) { cin >> s; int len = 32, p = 0; draw(p, 0, 0, len); } cout << "There are " << ans << " black pixels." << endl; } return 0; } |