poj 1610 Quad Trees

題意

四叉樹,就是模擬pr樹...不須要建樹,直接bfs就好了...由於沒注意要輸出大寫,wa了一遍又一遍→_→..另外提供一小組數據. 1 2 1 1 1 0 答案是154.

輸出是大寫的16進制....bfs

Quad Trees

Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 829
Accepted: 430

Description node

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 = 2 i 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 ide

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 = 2 i for some positive integer i. Then, an N * N binary array is followed in which at least one blank is between any two elements.

Output this

The bit stream (in hexadecimal) used to code each input array.

Sample Input spa

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 .net

0
114
258C0511

Source code

[Submit]   [Go Back]   [Status]   [Discuss] orm


個人解答:

/*=============================================================================
#     FileName: 1610.cpp
#         Desc: poj 1610
#       Author: zhuting
#        Email: cnjs.zhuting@gmail.com
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-05 14:54:47
#   LastChange: 2013-12-05 14:54:47
#      History:  
=============================================================================*/
/*
 * 16進制輸出的字母要大寫啊....
 */

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <string>
#include <cstring>
#define maxn 515
using namespace std;
int mymap[maxn][maxn] = {0};/*記錄點的顏色*/

bool ans[2*maxn*maxn] = {0};/*01序列*/
int ans_len = 0;

class area/*區域類*/
{
	public:
	int a1, b1, a2, b2;
	area(int a, int b, int c, int d)
	{
		a1 = a;
		b1 = b;
		a2 = c;
		b2 = d;
	}
};

queue <area> aq;/*用於bfs*/

void bfs(area ar)
{
	int x1 = ar.a1;
	int y1 = ar.b1;
	int x2 = ar.a2;
	int y2 = ar.b2;

	bool is_one = 0;/*是否有1*/
	bool is_zero = 0;/*是否有0*/
	bool is_div = 0;/*是否可分*/
	
	for (int i = x1; i <= x2; ++i)
	{
		if (is_one && is_zero)
			break;
		for (int j = y1; j <= y2; ++j)
		{
			if (is_one && is_zero)
				break;
			if (mymap[i][j]) is_one = 1;
			else is_zero = 1;
		}
	}
	if (is_one && is_zero)/*不全0不全1則可分*/
		is_div = 1;
	if(is_div)
	{
		int hx = (x1 + x2) >> 1;/*區間的中間座標*/
		int hy = (y1 + y2) >> 1;
		
		ans[ans_len++] = 1;

		aq.push(area(x1, y1, hx, hy));
		aq.push(area(x1, hy + 1, hx, y2));
		aq.push(area(hx + 1, y1, x2, hy));
		aq.push(area(hx + 1, hy + 1, x2, y2));
		return;
	}
		ans[ans_len++] = 0;
		ans[ans_len++] = is_one;
	return;
}


void trans()/*01序列轉16進制輸出*/
{
	int los = ans_len % 4;
	int tmp = 0;
	if (los)
	{
		for (int i = 0; i < los; ++i)
		{
			tmp += ans[i] << (los - i - 1);
		}
		printf("%X", tmp);
	}
	for (int i = los; i < ans_len; i += 4)
	{
		tmp = 0;
		for (int j = 0; j < 4; ++j)
		{
			tmp += ans[i + j] << (3 - j);
		}
		printf("%X", tmp);/*必定要大寫啊!!!*/
	}
	printf("\n");
	return;
}

void init()
{
	memset(mymap, 0, sizeof(mymap));
	memset(ans, 0, sizeof(ans));
	ans_len = 0;
	return;
}

int main()
{
	int t = 0, n = 0;
	scanf("%d", &t);
	while (t--)
	{
		init();
		scanf("%d", &n);
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				scanf("%d", &mymap[i][j]);
		aq.push(area(0, 0, n - 1, n - 1));

		while(!aq.empty())
		{
			area ar_tmp = aq.front();
			aq.pop();
			bfs(ar_tmp);
		}

		trans();

	}
	return 0;
}
相關文章
相關標籤/搜索