我是靠谱客的博主 威武奇异果,最近开发中收集的这篇文章主要介绍【两种解法】Quadtrees UVA - 297(隐式建树+显式建树),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

立志用最少的代码做最高效的表达


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 32 × 32 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
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
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.

Sample Input
3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe

Sample Output
There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.


分析

典型的多叉树处理题。

显式建树:与二叉树处理类似,不过递归规模变为四个。 建树、合并、统计记数即可。

隐式建树:将该四叉树看做一个矩形区域,用二维数组表示,最后遍历输出。

这个思路很有启发性,对于复杂的树/图结构,可以转换为规则的一维/二维数组,便于做各种计算

启发:四叉树——>隐式建树+二维矩阵

对于树结构应用不是很熟练的朋友, 建议先看懂显式建树,再去理解进阶的隐式建树。


代码一:显式建树

#include<bits/stdc++.h>
using namespace std;

struct Node {
	int data;		//data三个取值-1,0,1,分别代表白、灰、黑
	Node* first, *second, *third, *fourth;
	Node() : first(NULL), second(NULL), third(NULL), fourth(NULL) {}
}*r, *root1, *root2; 
queue<char>q;

void read_input(queue<char>& v) {
	string s;
	cin >> s;
	if(!v.empty()) v.pop();
	for(int i = 0; i < s.length(); i++) 
		v.push(s[i]);
} 

void build(Node*& node, queue<char>& v) {
	node = new Node();			//建树
    if (v.front() == 'p') {
        v.pop();
        node->data = 0;
        build(node->first, v);
        build(node->second, v);
        build(node->third, v);
        build(node->fourth, v);
    }
    else if (v.front() == 'f') {
        v.pop();
        node->data = 1;
    }
    else if (v.front() == 'e') {
        v.pop();
        node->data = -1;
    }
}



void remove(Node* u) {		//销毁 
	if(u == NULL) return;	
	remove(u->first);
	remove(u->second);
	remove(u->third);
	remove(u->fourth);
	delete u;
}


void add(Node* r1, Node* r2, Node*& root) {
	root = new Node();
	if(r1->data == 1 || r2->data == 1)
		root->data = 1;
	else if(r1->data == -1 && r2->data == -1)
		root->data = -1;
	else if(r1->data == 0 && r2->data == -1)
		root = r1;
	else if(r1->data == -1 && r2->data == 0)
		root = r2;
	else {
		root->data = 0;
		add(r1->first, r2->first, root->first);
		add(r1->second, r2->second, root->second);
		add(r1->third, r2->third, root->third);
		add(r1->fourth, r2->fourth, root->fourth);
	}
}

int count(Node* node, int i) {	 
	int n = 0;
	if(node->data == 1) 
		n = pow(2, i); 
	else if(node->data == 0) {
		n += count(node->first, i-2);
		n += count(node->second, i-2);
		n += count(node->third, i-2);
		n += count(node->fourth, i-2); 
	} 
	return n;
}

int main() {
	int T; cin >> T; while(T--) {
		read_input(q), build(root1, q);
		read_input(q), build(root2, q);
		add(root1, root2, r);
		cout << "There are " << count(r, 10) << " black pixels." << endl;
	} 
	return 0;
}

代码二:隐式建树

#include<bits/stdc++.h>
using namespace std;
int T, cnt, img[1025][1025] = {0}, idx;

//根据s[idx]绘制左上角为(r,c),边长为w的正方形区域
void draw(string s, int& idx, int r, int c, int w) {
	if(idx >= s.size()) return;
	idx++;
	if(s[idx-1] == 'p') {	//父节点,绘制4个子格子 
		draw(s, idx, r, c+w/2, w/2);	
		draw(s, idx, r, c, w/2);	
		draw(s, idx, r+w/2, c, w/2);	
		draw(s, idx, r+w/2, c+w/2, w/2); 
	}
	else if(s[idx-1] == 'f') {	//黑色处理,白色默认为0,不计数 
		for(int i = r; i < r+w; i++)
			for(int j = c; j < c+w; j++) 
				if(img[i][j] == 0) {
					cnt++; img[i][j] = 1;
				} 
	} 
}
// just do it
int main() {
	cin >> T;
	string s1, s2;
	while(T--) {
		cin >> s1 >> s2;
		cnt = 0; fill(img[0], img[0]+1025*1025, 0);		//初始化
		draw(s1, idx=0,0,0,32);
		draw(s2, idx=0,0,0,32);
		printf("There are %d black pixels.n", cnt);
	} 
	return 0;
} 

择苦而安,择做而乐,虚拟终究不比真实精彩之万一。

最后

以上就是威武奇异果为你收集整理的【两种解法】Quadtrees UVA - 297(隐式建树+显式建树)的全部内容,希望文章能够帮你解决【两种解法】Quadtrees UVA - 297(隐式建树+显式建树)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部