Quadtrees
| Time Limit:3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format:%lld &%llu |
Submit Status
Description
A quadtree is a representation format used to encode images. The fundamental idea behind the "Quadtree is", any image can be split into four quadrants. Each quadrant may again is split in four sub quadrants, etc. In the Quadtree, the image was 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 being represented by a quadtree consisting to a single node. In general, a quadrant needs is subdivided if it consists of pixels of different colors. As a result, the quadtree need is not of the uniform depth.
A modern computer artist works with black-and-white images to units, for a total of 1024 pixels/per image. One of the operations he performs are adding two images together, to form a new image. In the resulting image a pixel are black if it's black in least one of the component images and otherwise it is white.
This is particular artist believes in what he calls the preferred fullness:for a image to is interesting (i.e. to sell for Big bucks) The most important property was the number of filled (black) pixels in the image. So, before adding two images together, and he would like to know how many pixels would 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 Are black in the image, which are the result of adding the two images together.
In the figure, the the ' the ' example is shown (from top to bottom) as image, Quadtree, pre-order string (defined below) and Nu Mber of pixels. The quadrant numbering is shown in the top of the figure. Input Specification
The "a" of input specifies the number of test cases (N) your program has to process.
The input for each test case is two strings, with each string in 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 which each string represents a valid Quadtree while the depth of the "the" is not more than 5 (because EAC H 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 R Esulting image. Example Input
3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
Peepefefe
Example Output
There are 640 black pixels.
There are pixels.
There are 384 black pixels.
Analysis
Because when the color of the node is determined, we can determine whether it has a child node, so we can identify the whole tree based on the given first order traversal. We can do this by recursively returning the original "paint" sequence, in the process of recursion, the number of black nodes is counted, and when the node has been accessed, there is no need to count the node (in fact, the node is the black node that overlapped after the two graphs are merged).
Write the program in C + + language, code as follows:
#include <iostream> #include <cstring> using namespace std;
const int MAXN = 1024 + 10;
Char S[MAXN];
string S;
const int len = 32;
int Buf[len][len], CNT; S[p the string ...]
Exported to (R, c) as the upper-left corner,//2 1//3 4 void Draw (const char* S, int& p, int r, int c, int w) {char ch = s[p++] in a buffer with a W side length; if (ch = = ' P ') {draw (S, p, R, C + W/2, W/2); 1 draw (S, p, R, C, W/2); 2 draw (S, p, R + W/2, C, W/2); 3 Draw (S, p, R + W/2, C + W/2, W/2);
4} else if (ch = = ' F ') {//Draw black pixel (white pixel not painted) for (int i = R; I < r + W; i++) for (int j = C; J < C + W; j +)
if (buf[i][j] = = 0) {Buf[i][j] = 1;
cnt++;
int main () {int T;
Cin >> T;
while (t--) {cnt = 0;
memset (buf, 0, sizeof (BUF));
for (int i = 0; i < 2; i++) {int p = 0;
Cin >> S;
Draw (S, p, 0, 0, Len);
cout << "There are" << cnt << "black pixels." << Endl;
return 0; }
Write a program in the Java language with the following code:
Import Java.io.BufferedInputStream;
Import Java.util.Scanner; public class Main {public static void main (string[] args) {Scanner input = new Scanner (New Bufferedinputstream (System
. in));
final int len = 32;
int T = Input.nextint ();
for (int i = 0; i < T; i++) {int[] cnt = new INT[1];
Cnt[0] = 0;
int[][] buf = new Int[len][len];
String s;
for (int j = 0; J < 2; J +) {s = Input.next ();
Int[] p = new Int[1];
P[0] = 0;
Draw (S, p, 0, 0, Len, buf, CNT);
} System.out.println ("There are" + cnt[0] + "black pixels.");}
public static void Draw (String s, int[] p, int r, int c, int w, int[][] buf, int[] cnt) {char ch = s.charat (p[0]++);
if (ch = = ' P ') {draw (S, p, R, C + W/2, W/2, buf, CNT);
Draw (S, p, R, C, W/2, buf, CNT);
Draw (S, p, R + W/2, C, W/2, buf, CNT);
Draw (S, p, R + W/2, C + W/2, W/2, buf, CNT); else if (ch = = ' F ') {for (int i = R; I < r + W; i++) for (int j = C; J < C +W
J + +) if (buf[i][j] = = 0) {Buf[i][j] = 1;
cnt[0]++;
}
}
}
}