【两种解法】Quadtrees UVA - 297(隐式建树+显式建树)
立志用最少的代碼做最高效的表達(dá)
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.
分析
典型的多叉樹處理題。
顯式建樹:與二叉樹處理類似,不過遞歸規(guī)模變?yōu)樗膫€(gè)。 建樹、合并、統(tǒng)計(jì)記數(shù)即可。
隱式建樹:將該四叉樹看做一個(gè)矩形區(qū)域,用二維數(shù)組表示,最后遍歷輸出。
這個(gè)思路很有啟發(fā)性,對(duì)于復(fù)雜的樹/圖結(jié)構(gòu),可以轉(zhuǎn)換為規(guī)則的一維/二維數(shù)組,便于做各種計(jì)算
啟發(fā):四叉樹——>隱式建樹+二維矩陣
對(duì)于樹結(jié)構(gòu)應(yīng)用不是很熟練的朋友, 建議先看懂顯式建樹,再去理解進(jìn)階的隱式建樹。
代碼一:顯式建樹
#include<bits/stdc++.h> using namespace std;struct Node {int data; //data三個(gè)取值-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;//根據(jù)s[idx]繪制左上角為(r,c),邊長為w的正方形區(qū)域 void draw(string s, int& idx, int r, int c, int w) {if(idx >= s.size()) return;idx++;if(s[idx-1] == 'p') { //父節(jié)點(diǎn),繪制4個(gè)子格子 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') { //黑色處理,白色默認(rèn)為0,不計(jì)數(shù) 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; }擇苦而安,擇做而樂,虛擬終究不比真實(shí)精彩之萬一。
總結(jié)
以上是生活随笔為你收集整理的【两种解法】Quadtrees UVA - 297(隐式建树+显式建树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【通俗易懂】理解Python中的if _
- 下一篇: 【最新合集】编译原理习题(含答案)_15