uva806 Spatial Structures

  Spatial Structures 

Computer graphics, image processing, and GIS (geographic information systems) all make use of a data structure called a quadtree. 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 is constructed by successively dividing the image into four equal quadrants. If all the pixels in a quadrant are the same color (all black or all white) the division process for that quadrant stops. Quadrants that contain both black and white pixels are subdivided into four equal quadrants and this process continues until each subquadrant consists of either all black or all white pixels. It is entirely possible that some subquadrants consist of a single pixel.

For example, using 0 for white and 1 for black, the region on the left below is represented by the matrix of zeros and ones in the middle. The matrix is divided into subquadrants as shown on the right where gray squares represent subquadrants that consist entirely of black pixels.

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

Leaf nodes are white if they correspond to a block of all white pixels, and black if they correspond to a block of all black pixels. In the tree, each leaf node is numbered corresponding to the block it represents in the diagram above. The branches of a non-leaf node are ordered from left-to-right as shown for the northwest, northeast, southwest, and southeast 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 is a base five number constructed by labeling branches with 1, 2, 3, or 4 with NW = 1, NE = 2, SW = 3, SE = 4, and with the least significant digit of the base five number corresponding to the branch from the root. For example, the node labeled 4 has path NE, SW which is 325 (base 5) or 1710 (base 10); the node labeled 12 has path SW, SE or 435 = 2310 ; and the node labeled 15 has path SE, SW, NW or 1345 = 4410 . The entire tree is represented by the sequence of numbers (in base 10)

9 14 17 22 23 44 63 69 88 94 113

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

Input

The input contains one or more images. Each image is square, and the data for an image starts with an integer n, where | n| is the length of a side of the square (always a power of two, 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 that represent the root-to-leaf paths of each black node in the quadtree that represents the image.

If n is positive, the zero/one representation follows; if n is negative, the sequence of black node path numbers (in base 10) follows. The sequence is terminated by the number -1. A one-node tree that represents an all-black image is represented by the number 0. A one-node tree that represents an all-white image is 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 quadtree that represents the image. The values should be base 10 representations of the base 5 path numbers, and the values should be printed in sorted order. If there are more than 12 black nodes, print a newline after every 12 nodes. The total number of black nodes should be 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 the image with the character `.‘ used for white/zero and the character `*‘ used for black/one. There should be n characters per line for an n×n image.

Print a blank line between cases.

Sample Input

8
00000000
00000000
00001111
00001111
00011111
00111111
00111100
00111000
-8
9 14 17 22 23 44 63 69 88 94 113 -1
2
00
00
-4
0 -1
0

Sample Output

Image 1
9 14 17 22 23 44 63 69 88 94 113
Total number of black nodes = 11

Image 2
........
........
....****
....****
...*****
..******
..****..
..***...

Image 3
Total number of black nodes = 0

Image 4
****
****
****
****

这题目就是给你两种表示方法让你互相转化,写的时候没注意每12个要换行,WA了3发。直接递归写的,避免了建树。不知道uva支不支持#include<bits/stdc++.h>。提交的时候注释掉了。
我要开始装B,写copyright,不要管我。

(。?`ω´?)



/*
Created by abyssal fish on 2015.6.30
Copyright (c) 2015 Rey . All rights reserved.
*/

#include<cstdio>
//#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
const int maxn = 65;

char G[maxn][maxn];

int path[maxn*maxn];
int sz;

void solve1(int r1,int c1,int r2,int c2,int wei,int sum)
{
   bool whi, bla; whi = bla = false;
   for(int i = r1;i <= r2;i++)
      for(int j = c1;j <= c2;j++){
         if(!bla && G[i][j] == ‘1‘) bla = true;
         if(!whi && G[i][j] == ‘0‘) whi = true;
         if(whi&&bla) { break;}
      }
   if(bla) {
      if(!whi) {path[++sz] = sum; return;}
   }else if(whi) return;

   int dvr = r1+r2>>1, dvc = c1+c2>>1;
      int nwei = 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));
}

int a[20];
void draw(int n,int r1,int c1,int r2,int c2){
   int Sz = 0;
   while(n){ a[Sz++]=n%5;n/=5;}
   for(int i = 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(int i = r1;i <= r2;i++)
      for(int j = c1;j <= c2;j++)
         G[i][j] = ‘*‘;
}

int main()
{
   int n;
   int Cas = 0;
   while(~scanf("%d",&n)&&n){
      if(Cas) puts("");
      if(n>0){
         for(int i = 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(int i = 1; i < sz; i++)//PE ,bi dog
            printf("%d%c",path[i],i%12?‘ ‘:‘\n‘);
         if(sz)printf("%d\n",path[sz]);
         printf("Total number of black nodes = %d\n",sz);
      }else {
         n = -n;
         int t;
         for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++)
               G[i][j]=‘.‘;
            G[i][n] = ‘\0‘;
         }

         while(scanf("%d",&t)&&~t){
            draw(t,0,0,n-1,n-1);
         }
         printf("Image %d\n",++Cas);
         for(int i = 0;i < n;i++)
            puts(G[i]);
      }

   }
   return 0;
}
				
时间: 2024-08-06 02:05:19

uva806 Spatial Structures的相关文章

UVA-806 Spatial Structures (四分树)

题目大意:将一块图像上的黑点在两种表示法之间转换. 题目分析:递归下去... 注意:输出时要注意细节!!! 代码如下: # include<iostream> # include<cstdio> # include<vector> # include<string> # include<cstring> # include<algorithm> using namespace std; char p[80][80]; int ans;

UVa-806 Spatial Structures(四分树)

代码借鉴:http://www.cnblogs.com/npugen/p/9527453.html 1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 using namespace std; 4 //1 2 5 //3 4 6 7 const int maxn = 70; 8 int n,cnt; 9 char gra[maxn][maxn]; 10 vector<int

806 - Spatial Structures(DFS)

没什么思路,难就难在麻烦,各种DFS,挺练基本功的...   Problem   Verdict Lang Time Best Rank Submit Time  | discuss806 - Spatial Structures  Accepted C++ 0.272 0.045 90 2 mins ago 中间被卡了一次,所以出了好多数据 QAQ,都填在这里吧 #include<cstdio> #include<vector> #include<cstring> #

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

在spatial扩展和3D扩展都带这一工具

ArcGIS水分分析工具的流向分析是基于D8单流向算法,如果分析使用的DEM存在凹陷点,就会产生汇,导致径流断流从而影响了分析结果.在前面章节<ArcGIS水文分析实战教程(2)ArcGIS水文分析工具的基本原理>中又介绍过D8算法,而<ArcGIS水文分析实战教程(4)地形预处理>章节中笔者也较少过如何创建无凹陷点得DEM数据,在使用流向分析工具之前可以先行阅读. 首先流向分析要使用填洼过的数据,确保DEM数据没有凹陷点.如果数据准备妥当,直接使用水文分析工具箱中的[流向]工具进

【DataStructure】Linked Data Structures

Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure. so inserting an elemen

Scala Control Structures

Scala之Control Structures 一.前言 前面学习了Scala的Numbers,接着学习Scala的Control Structures(控制结构). 二.Control Structures Scala中的控制结构与Java中的颇为类似,但也有所不同,例如,if/then/else控制结构与Java的类似,但是其可以返回值,虽然Java中有三元运算符的特殊语法,但是在Scala中使用if就可以达到同样的效果. val x = if (a) y else z 同样,Scala的

[转]ArcIMS 中地图坐标参考设置(ArcGIS Unknown Spatial Reference)

"ArcGIS Unknown Spatial Reference"问题: shp文件在Arcgis打开后经常因为原有坐标系无法识别而丢失信息,出现以下提示信息: "Unknown Spatial Reference The following data sources you added are missing spatial reference information. This data can be drawn in ArcMap, but cannot be pro

Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记(转)

在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 转自:http://blog.csdn.net/xzzppp/article/details/51377731 另可参考:http://zhangliliang.com/2014/09/13/paper-note-sppnet/ http:/