Quad Trees
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1114 | Accepted: 537 |
Description
A binary image, such as the one shown in Figure 2(a), is usually represented as an array of binary entries, i.e., each entry of the array has value 0 or 1. Figure 2(b) shows the array that represents the binary image in Figure 2(a). To store the binary image of Figure 2(b), the so-called quad tree partition is usually used. For an N * N array,N <= 512 and N = 2i for some positive integer i, if the entries do not have the same value, then it is partitioned into four N/2 * N/2 arrays, as shown in Figure 2(c). If an N/2*N/2 array does not have the same binary value, such as the upper right and
lower right N/2*N/2 arrays in Figure 2(c), then we can divide it into four N/4*N/4 arrays again. These N/4*N/4 arrays in turn can also, if needed, be divided into four N/8 * N/8 arrays, etc.. The quad tree partition is completed when the whole array
is partitioned into arrays of various size in which each array contains only one binary value. Figure 2(c) contains the arrays after the quad tree partition is completed.
Instead of storing the binary image of Figure 2(a), we only need to store the quad tree in the form as Figure 2(d) which is encoded from Figure 2(c). In Figure 2(d), each node represents an array of Figure 2(c) in which the root node represents the original array. If the value of a node in the tree is 1, then it means that its corresponding array needs to be decomposed into four smaller arrays. Otherwise, a node will have a pair of values and the first one is 0. It means that its corresponding array is not necessary to decompose any more. In this case, the second value is 0 (respectively,1) to indicate that all the entries in the array are 0 (respectively, 1). Thus, we only need to store the tree of Figure 2(d) to replace storing the binary image of Figure 2(a). The way to store the tree of Figure 2(d) can be represented by the following
Figure 2: A binary image (a), its array representation (b), its quad tree partition (c),and its quad tree representation (d).
code: (1)(0,0)(1)(0,1)(1)(0,0)(0,1)(1)(0,0)(0,0)(0,0)(0,1)(0,1)(0,0)(0,1)(0,0)(0,1). This
code is just to list the values of the nodes from the root to leaves and from left to right in each level. Deleting the parentheses and commas, we can obtain a binary number 100101100011000000010100010001 which is equal to 258C0511 in hexadecimal. You are asked to design a program for finding the resulting hexadecimal value for each given image.
Input
There is an integer number k, 1 <= k <= 100, in the first line to indicate the number of test cases. In each test case, the first line is also a positive integer N indicating that the binary image is an N * N array, where N <= 512 and N = 2ifor some positive integer i. Then, an N * N binary array is followed in which at least one blank is between any two elements.
Output
The bit stream (in hexadecimal) used to code each input array.
Sample Input
3 2 0 0 0 0 4 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 8 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output
0 114 258C0511
Source
题目的意思是给你一个bitmap图,你要把它不断地四分,然后用四叉树的形式存储这个图。
一看题目觉得有点类似于图形学中的八叉树来表示模型。那就不断递归地划分图,建立一个四叉树的数据结构去存储此图。题目要求四叉树层次遍历得到一个二进制序列,那就用队列及堆栈来实现,存储在ans[]数组中,ans是反序存放答案。
16进制输出要大写。。。。嗯。有点坑