Exercise 6-8 Spatial Structure UVa806

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-solving ideas: The subject to examine the four-minute tree traversal. The subject is known as a four-point tree for the number of black nodes; the number of known black nodes in turn constructs a four-minute tree. The first case is equivalent to coding, we use the first order traversal to solve. The Encode function is used to encode the entire four-minute tree, and if it is found that both 0 and 1 appear internally, it is divided into four parts, recursively encoded, and conversely, the color and path of the node are counted. This encodes the path into a decimal number for easy conversion. Similarly, if a black node is known, the path of each point is decoded first, and then the origin of the location is located along its path from the source of the coordinates, which is then marked as ' * '.

Note: To see the output format, for the first case, full 12 to wrap.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace Std;const int N = + 5;char G[n][n];vector <int>code;int n;struct node{int color;int dir;} node[n*n];void encode (int r, int c, int w, int dir, int &id)//represent: Start coordinate r, starting point coordinate C, Width w, encoded direction, node number {int OK = 1;char ch = g[r ][c];for (int i = R; I < r + W; i++) for (int j = C; J < C + W; + j) if (g[i][j]! = ch) {ok = 0; break;}  if (OK)//found a node {node[id].color = (ch = = ' 0 '? 0:1); int x = dir, k = 1;stack<int>s;while (x>0) {s.push (x%); x /= 10;} while (!s.empty ())//use a stack to flip 5 binary number to convert to 10 binary {node[id].dir + = S.top () *k; S.pop (); k *= 5;} id++;} else//recursive encoding {encode (R, C, W/2, dir * + 1, id); EncoDe (R, C + W/2, W/2, dir * + 2, id); Encode (R + W/2, C, W/2, dir * + 3, id); Encode (R + W/2, C + W/2, W/2 , dir * + 4, id);}} void Draw (int r, int c, int w) {for (int i = R; I < r + W; i++.) for (int j = C; J < C + W; b + +) g[i][j] = ' * ';} void Decode (vector<int> s) {int len = s.size (); for (int i = 0; i < len; i++) {int x = s[i], k = 1;node[i].color = 1 while (x>0)//decode the path, low is the lower path {node[i].dir + = (x% 5) *k;x/= 5, K *= 10;}}  for (int i = 0; i < len; i++) {int dir = Node[i].dir;int r = 0, c = 0, W = n;while (dir>0)//find the corresponding area along the path {int x = dir% 10;if (x = = 1) w/= 2;if (x = = 2) {c + = W/2; w/= 2;} if (x = = 3) {r + = W/2; w/= 2;} if (x = = 4) {r + = W/2; c + = W/2; w/= 2;} Dir/= 10;} Draw (R, C, W);} for (int i = 0, i < n; i++) for (int j = 0; J < N; j + +) if (g[i][j]! = ' * ') g[i][j] = '. '; for (int i = 0; i < n; i++) puts (G[i]);} int main () {//freopen ("T.txt", "R", stdin); int rnd = 0;while (~scanf ("%d", &n) && N) {memset (g, ' + ', sizeof (g)) CodE.clear (); memset (node, 0, sizeof (node)), if (RND) puts ("");p rintf ("Image%d\n", ++rnd), if (n > 0) {for (int i = 0; I < n; i++) scanf ("%s", G[i]), int id = 1;encode (0, 0, N, 0, id);//encode the entire image vector<int>tmp;for (int i = 1; i < ID; i++) if (Node[i].color = = 1) tmp.push_back (node[i].dir); sort (Tmp.begin (), Tmp.end ()); for (int i = 0; i < tmp.size (); i++) {print F ("%d", tmp[i]); if ((i + 1)% = = 0 | | i = = tmp.size ()-1) printf ("\ n"), Else printf (""); printf ("Total number of black nodes =%d\n", tmp.size ());} Else{n =-n;int X;while (~scanf ("%d", &x) && x! =-1) code.push_back (x);d Ecode (code);//decoding after drawing}}return 0;}

Exercise 6-8 Spatial Structure UVa806

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.