<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">K - </span><span style="color: blue; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Quadtrees</span>
Time limit:3000 Ms
Memory limit:0 KB
64bit Io format:% LLD & % llusubmit statusappoint description: System crawler)
Description
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 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 participant ular artist believes in what he callthePreferred fullness: For an image to be interesting (I. e. to upload for big bucks) The most important property is the number of filled (black) pixels in the image. so, before adding two images together, he wowould 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 areXBlack pixels.', WhereXIs the number of black pixels in the resulting image.
Example Input
3ppeeefpffeefepefepeefepeeefpeefepeeefpeepefefe
Example output
There are 640 black pixels.There are 512 black pixels.There are 384 black pixels.
There is a graph represented by a quad-tree. This graph is represented by P, E, F, P, parent node, F, black, and E, respectively, the size of the entire graph is 1024. Each subgraph can be divided into four parts (only when the color is different). Now we need to combine the two graphs into one and find the number of black pixels in the merged graph.
# Include <stdio. h ># include <cstring> # include <algorithm> int t; char S1 [2049], S2 [2049]; struct quadtree {int num; quadtree * Next [4]; quadtree () {num = 0; For (INT I = 0; I <4; I ++) next [I] = 0 ;}}; quadtree * build (char * s) // build {quadtree * Now = new quadtree; int Len = strlen (s); If (s [0]! = 'P') {now-> num = 1; if (s [0]! = 'F') {Delete now; now = NULL;} return now;} int up = 4; // Number of subtree int d = 1; for (INT I = 1; D <= up & I <Len; I ++) {If (s [I] = 'P ') {now-> next [D-1] = build (S + I); int dx = 0, dy = 4; while (DX <Dy) {DX ++; if (s [I + dx] = 'P') dy + = 4;} I + = DX; // change I to the starting position of the next subtree} else {now-> next [D-1] = build (S + I);} d ++ ;} return now;} quadtree * merge _ (quadtree * P, quadtree * q) // merge {If (p | q) {quadtree * root = new quadtree; if (P & Q) for (INT I = 0; I <4; I ++) {If (p-> num | Q-> num) {root-> num = 1; // the subtree is black and does not require recursive continue ;} root-> next [I] = merge _ (p-> next [I], Q-> next [I]);} else if (P = NULL & Q) for (INT I = 0; I <4; I ++) {If (Q-> num) {root-> num = 1 ;; /// the subtree is already black and does not need to be recursively continue;} root-> next [I] = merge _ (null, Q-> next [I]);} else For (INT I = 0; I <4; I ++) {If (p-> num) {root-> num = 1 ;; /// the subtree is black and does not need to be recursive.} root-> next [I] = merge _ (p-> next [I], null );} return root;} return NULL;} int DFS (quadtree * P, int num) {If (P = NULL) return 0; int sum = 0; if (p-> num) sum + = num; For (INT I = 0; I <4; I ++) {sum + = DFS (p-> next [I], num/4);} return sum;} int main () {// freopen ("in.txt ", "r", stdin); quadtree * root1, * root2, * root; scanf ("% d", & T); While (t --) {scanf ("% S % s", S1, S2); root = root1 = root2 = NULL; root1 = build (S1); root2 = build (S2 ); root = merge _ (root1, root2); printf ("there are % d black pixels. \ n ", DFS (root, 1024);} return 0 ;}
UV 297 quadtrees (build, merge, and traverse a quad-tree)