297 – QuadtreesTime limit: 3.000 seconds |
Quadtrees |
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 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 Specification
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 Specification
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.
Example Input
1 2 3 4 5 6 7 |
3 ppeeefpffeefe pefepeefe peeef peefe peeef peepefefe |
Example Output
1 2 3 |
There are 640 black pixels. There are 512 black pixels. There are 384 black pixels. |
把两条树合在一起只需要把建两次树,然后每次建树都是对同一颗树进行操作就可以了,感觉树相关的题目真心好用来巩固指针知识。
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
/******************************************************** * File Name: uvaoj297.cpp * Author: razrLeLe * Mail: razrlele@outlook.com * Homepage: https://yueyu.io * Created Time: Thu 29 Jan 2015 01:37:54 PM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 20000; string in; int p, n; struct node { int color; node *child[4]; }; node Tree[maxn]; inline node* NewNode() { Tree[n].color = 0; for(int i = 0; i < 4; i++) { Tree[n].child[i] = NULL; } return &Tree[n++]; } node* BuildTree(node* root) { if(p == in.length()) return NULL; char order = in[p++]; if(root != NULL) if(root->color == 1) return root; if(root == NULL) root = NewNode(); if(order == 'p') { for(int i = 0; i < 4; i++) { root->child[i] = BuildTree(root->child[i]); } } else if(order == 'f') { root->color = 1; for(int i = 0; i < 4; i++) { root->child[i] = NULL; } } return root; } void DFS(node* root, int depth, int &ans) { if(root->color == 1) { ans += depth*depth; return ; } if(root->child[0] != NULL) { for(int i = 0; i < 4; i++) DFS(root->child[i], depth/2, ans); } return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); #endif int numcase; while(cin >> numcase) { while(numcase--) { p = n = 0; cin >> in; node *root = NULL; int ans = 0; root = BuildTree(root); cin >> in; p = 0; root = BuildTree(root); DFS(root, 32, ans); printf("There are %d black pixels.\n", ans); } } return 0; } |