UVA806 Spatial Structures

Source: Internet
Author: User

Spatial Structures

Computer graphics, image processing, and GIS (geographic information systems) all do use of a data structure called a Qu Adtree. Quadtrees represent regional or block data efficiently and support efficient algorithms for operations like the Union and Intersection of images.

A quadtree for a black and white image are constructed by successively dividing the image to four equal quadrants. If all of the pixels in a quadrant is the same color (All Black or all white) the division process for that quadrant stops. Quadrants that contain both black and white pixels is subdivided into four equal quadrants and this process continues UNT Il each subquadrant consists of either all black or all white pixels. It is entirely possible, some subquadrants consist of a single pixel.

For example, using 0 for White and 1 for Black, the region on the left below was represented by the Matri X of zeros and ones in the middle. The matrix is divided to subquadrants as shown on the right where gray squares represent subquadrants that consist Entir Ely of black pixels.

A Quadtree is constructed from the block structure by an image. The root of the tree represents the entire array of pixels. Each non-leaf node of a quadtree have four children, corresponding to the four subquadrants of the region represented by th E node. Leaf nodes represent regions that consist of pixels of the same color and thus is not subdivided. For example, the image shown above, and the block structure on the right, are represented by the quadtree below.

Leaf nodes is white if they correspond to a block of any white pixels, and black if they correspond to a block of all bla ck pixels. The tree, each leaf node is numbered corresponding to the block it represents in the diagram above. The branches of a non-leaf node is ordered from Left-to-right as shown for the northwest, northeast, southwest, and south East quadrants (or Upper-left, Upper-right, Lower-left, Lower-right).

A tree can be represented by a sequence of numbers representing the root-to-leaf paths of black nodes. Each path was a base five number constructed by labeling branches with 1, 2, 3, or 4 with NW = 1, NE = 2, SW = 3, SE = 4, a nd with the least significant digit of the base five number corresponding to the branch from the root. For example, the node labeled 4 have path NE, SW which is 325 (base 5) or 1710 (base ten); the node labeled has P Ath SW, SE or 435 = 2310, and the node labeled has path SE, SW, NW or 1345 = 4410. The entire tree is represented by the sequence of numbers (in base)

9 14 17 22 23 44 63 69 88 94 113

Write A program that converts images to root-to-leaf paths and converts root-to-leaf paths into images.

Input

The input contains one or more images. Each image was square, and the data for a image starts with an integer n, where | N| Is the length of a side of the square (always a power of both, with | N| ≤64) followed by a representation of the image. A representation is either a sequence of n2 zeros and ones comprised of | N| Lines of | N| digits per line, or the sequence of numbers this represent the root-to-leaf paths of each black node in the Quadtree Represents the image.

If n is positive, the zero/one representation follows; if n is negative, the sequence of black n Ode path numbers (in base) follows. The sequence is terminated by the number -1. A One-node tree, represents an all-black image was represented by the number 0. A One-node tree, represents an all-white image was represented by an empty sequence (no numbers).

The end of data is signaled by a value of 0 for n.

Output

For each image in the input, first output the number of the image, as shown in the sample output. Then output the alternate form of the image.

If the image is represented by zeros and ones, the output consists of root-to-leaf paths of all black nodes in the Quadtre E that represents the image. The values should be base representations of the base 5 path numbers, and the values should is printed in sorted order. If there is more than the black nodes, print a newline after every nodes. The total number of black nodes should is printed after the path numbers.

If the image is represented by the root-to-leaf paths of black nodes, the output consists of an ASCII representation of th e image with the character '.' used for White/zero and the character '*' used for Black/one. There should is n characters per line for an nxn image.

Print a blank line between cases.

Sample Input
80000000000000000000011110000111100011111001111110011110000111000-89 14 17 22 23 44 63 69 88 94 113-120000-40-10

Sample Output
Image: 94 113Total number of black nodes = 11Image 2....................****....****...*****. ******.. ****....***... Image 3Total number of black nodes = 0Image 4****************

This topic is to give you two ways to make you transform each other, write the time did not notice every 12 to change line, WA 3 rounds.
the direct recursive write, avoids the achievement. It is not known that UVA support does not support #include<bits/stdc++.h>. The comment was dropped at the time of submission.

I will start to install B, write the copyright, do not care about me.
(.? ' Ω´?)
/*Created by Abyssal fish on 2015.6.30Copyright (c) Rey. All rights reserved.*/#include<cstdio>//#include <bits/stdc++.h>#include <algorithm>using namespacestd;Const intMAXN = $;CharG[MAXN][MAXN];intpath[maxn*MAXN];intsz;voidSolve1 (intR1,intC1,intR2,intC2,intWeiintsum) {   BOOLWHI, bla; WHI = Bla =false;  for(inti = R1;i <= r2;i++)       for(intj = C1;j <= c2;j++){         if(!bla && g[i][j] = ='1') Bla =true; if(!whi && g[i][j] = ='0') WHI =true; if(WHI&AMP;&AMP;BLA) { Break;} }   if(BLA) {if(!WHI) {Path[++sz] = sum;return;} }Else if(WHI)return; intDVR = r1+r2>>1, DVC = c1+c2>>1; intNwei =5*Wei; Solve1 (R1, C1,dvr,dvc,nwei,sum+Wei); Solve1 (R1,DVC+1, DVR, c2,nwei,sum+ (wei<<1)); Solve1 (DVR+1, C1, r2,dvc,nwei,sum+ wei*3 ); Solve1 (DVR+1, dvc+1, R2, c2,nwei,sum+ (wei<<2));}inta[ -];voidDrawintNintR1,intC1,intR2,intC2) {   intSz =0;  while(n) {a[sz++]=n%5; n/=5;}  for(inti =0; I < sz;i++){      Switch(A[i]) { Case 1: {R2= r1+r2>>1; C2 = c1+c2>>1;  Break; }          Case 2: {R2= r1+r2>>1; C1 = (c1+c2>>1)+1;  Break; }          Case 3: {R1= (r1+r2>>1)+1; C2 = c1+c2>>1;  Break; }          Case 4: {R1= (r1+r2>>1)+1; C1 = (c1+c2>>1)+1;  Break; }      }   }    for(inti = R1;i <= r2;i++)       for(intj = C1;j <= c2;j++) G[i][j]='*';}intMain () {intN; intCas =0;  while(~SCANF ("%d", &n) &&N) {      if(Cas) puts (""); if(n>0){          for(inti =0; I < n; i++) scanf ("%s", G[i]); SZ=0; Solve1 (0,0, N-1, N-1,1,0); Sort (Path+1, path+sz+1); printf ("Image%d\n",++Cas);  for(inti =1; I < sz; i++)//PE, Bi dogprintf"%d%c", path[i],i% A?' ':'\ n'); if(SZ) printf ("%d\n", Path[sz]); printf ("Total number of black nodes =%d\n", SZ); }Else{n= -N; intT;  for(inti =0; I < n;i++){             for(intj =0; J < n;j++) G[i][j]='.'; G[i][n]=' /'; }          while(SCANF ("%d", &t) &&~t) {Draw (T,0,0, N-1, N-1); } printf ("Image%d\n",++Cas);  for(inti =0; I < n;i++) puts (g[i]); }   }   return 0;}






UVA806 Spatial Structures

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.