Quad Trees - ZOJ 1788 四分树

原创 2015年07月08日 18:19:21

Quad Trees

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A binary image, such as the one shown in Figure 2(a), is usually represented as an array of binary entries, i.e., each entry of the array has value 0 or 1. Figure 2(b) shows the array that represents the binary image in Figure 2(a). To store the binary image of Figure 2(b), the so-called quad tree partition is usually used. For an N �� N array, N <= 512 and N = 2^i for some positive integer i, if the entries do not have the same value, then it is partitioned into four N/2 �� N/2 arrays, as shown in Figure 2(c). If an N/2��N/2 array does not have the same binary value, such as the upper right and lower right N/2��N/2 arrays in Figure 2(c), then we can divide it into four N/4��N/4 arrays again. These N/4��N/4 arrays in turn can also, if needed, be divided into four N/8 �� N/8 arrays, etc.. The quad tree partition is completed when the whole array is partitioned into arrays of various size in which each array contains only one binary value. Figure 2(c) contains the arrays after the quad tree partition is completed.


Figure 2: A binary image (a), its array representation (b), its quad tree partition (c), and its quad tree representation (d).

Instead of storing the binary image of Figure 2(a), we only need to store the quad tree in the form as Figure 2(d) which is encoded from Figure 2(c). In Figure 2(d), each node represents an array of Figure 2(c) in which the root node represents the original array. If the value of a node in the tree is 1, then it means that its corresponding array needs to be decomposed into four smaller arrays. Otherwise, a node will have a pair of values and the first one is 0. It means that its corresponding array is not necessary to decompose any more. In this case, the second value is 0 (respectively, 1) to indicate that all the entries in the array are 0 (respectively, 1). Thus, we only need to store the tree of Figure 2(d) to replace storing the binary image of Figure 2(a). The way to store the tree of Figure 2(d) can be represented by the following code:

(1)(0,0)(1)(0,1)(1)(0,0)(0,1)(1)(0,0)(0,0)(0,0)(0,1)(0,1)(0,0)(0,1)(0,0)(0,1).

This code is just to list the values of the nodes from the root to leaves and from left to right in each level. Deleting the parentheses and commas, we can obtain a binary number 100101100011000000010100010001 which is equal to 258C0511 in hexadecimal. You are asked to design a program for finding the resulting hexadecimal value for each given image.


Input

There is an integer number k, 1 <= k <= 100, in the first line to indicate the number of test cases. In each test case, the first line is also a positive integer N indicating that the binary image is an N �� N array, where N <= 512 and N = 2^i for some positive integer i. Then, an N �� N binary array is followed in which at least one blank is between any two elements.


Output

The bit stream (in hexadecimal) used to code each input array.


Sample Input

3
2
0 0
0 0
4
0 0 1 1
0 0 1 1
1 1 0 0
1 1 0 0
8
0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1


Sample Output

0
114
258C0511


题意:按照图示对一个n*n的矩阵进行建树,建完树之后把每层的数字都连接起来,组成一个01的二进制数,然后转化为16进制。

思路:按照四分树的思想去进行dfs。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int T,t,n,num[520][520],sum[520][520];
string str[12],str2,str3,ans;
char trans[30]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int check(int x,int y,int len)
{
    int i,j,k,x2=x+len-1,y2=y+len-1;
    k=sum[x2][y2]-sum[x-1][y2]-sum[x2][y-1]+sum[x-1][y-1];
    if(k==0)
      return 0;
    if(k==len*len)
      return 1;
    return -1;
}
void dfs(int x,int y,int len,int p)
{
    int i,j,k;
    k=check(x,y,len);
    if(k==-1)
    {
        str[p]+="1";
        len/=2;
        dfs(x,y,len,p+1);
        dfs(x,y+len,len,p+1);
        dfs(x+len,y,len,p+1);
        dfs(x+len,y+len,len,p+1);
    }
    else if(k==0)
      str[p]+="00";
    else if(k==1)
      str[p]+="01";
}
void print()
{
    int i,j,k,len,ret;
    str2=str3=ans="";
    for(i=1;i<=10;i++)
       str2+=str[i];
    len=str2.length();
    if(len%4!=0)
      for(i=1;i<=4-len%4;i++)
         str3+="0";
    str3+=str2;
    len=str3.length();
    for(i=3;i<len;i+=4)
    {
        ret=0;
        for(j=0;j<4;j++)
           ret+=(str3[i-j]-'0')<<j;
        printf("%c",trans[ret]);
    }
    printf("\n");
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
              scanf("%d",&num[i][j]);
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
              sum[i][j]=sum[i][j-1]+num[i][j];
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
              sum[i][j]=sum[i-1][j]+sum[i][j];
        for(i=0;i<=10;i++)
           str[i]="";
        dfs(1,1,n,1);
        print();
    }
}



poj1623 象限四分树

Squadtrees Time Limit: 10000MS   Memory Limit: 32768K Total Submissions: 389   Acc...
  • u012866104
  • u012866104
  • 2015年06月22日 14:21
  • 1080

ZOJ,PKU--训练题分类

ZOJ题目分类  初学者题:  1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 ...
  • shao1996
  • shao1996
  • 2016年05月27日 21:43
  • 714

2D空间中使用Quadtree四叉树进行碰撞检测优化

很多游戏中都需要使用碰撞检测算法检测两个物体的碰撞,但通常这些碰撞检测算法都很耗时很容易拖慢游戏的速度。这里我们学习一下使用四叉树来对碰撞检测进行优化,优化的根本是碰撞检测时跳过那些明显离得很远的物体...
  • cordova
  • cordova
  • 2016年12月22日 22:44
  • 2695

UVA—297四分树(非二叉树)

题意: 用一个四分树来表示一个黑白图像,方法是用根节点来表示整个图像,然后再进行等分,按照从右上角的图像开始逆时针的顺序编 号,依次有四个节点,如果某子节点对应的区域是全黑或者全白,则直接用黑或者...
  • shao1996
  • shao1996
  • 2016年05月27日 21:27
  • 933

四叉树空间索引原理及其实现

                   今天依然在放假中,在此将以前在学校写的四叉树的东西拿出来和大家分享。 四叉树索引的基本思想是将地理空间递归划分为不同层次的树结构。它将已...
  • zhanghefu
  • zhanghefu
  • 2014年05月17日 09:02
  • 6761

习题6-8 空间结构 UVa806

1.题目描述:点击打开链接 2.解题思路:本题考查四分树的遍历。本题实质上是已知一棵四分树求黑色结点的编号;已知黑色结点的编号反过来构造四分树。第一种情况相当于编码,我们用先序遍历解决。用encod...
  • u014800748
  • u014800748
  • 2015年04月04日 01:29
  • 1040

UVa 297.Quadtrees【非二叉树之四分树】【7月31】

Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behin...
  • a995549572
  • a995549572
  • 2015年07月31日 16:17
  • 611

zoj3772--【水题】线段树区间查询+矩阵乘法

思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments 【思路不是自己的,但代码是自己敲的,由于伦家不懂】...
  • u011652573
  • u011652573
  • 2014年04月08日 21:24
  • 696

zoj-2112(主席树动态求区间第k小数)

总算是把动态求区间第k个数的算法看明白了。 在主席树的基础上,如果有修改操作,则要通过套树状数组来实现任意区间求第k小的问题。 刚开始看不明白什么意思,现在有一点理解。树状数组的每个元素是一个线段...
  • u014492306
  • u014492306
  • 2015年08月25日 19:30
  • 1260

ZOJ 2112 Dynamic Rankings(主席树套树状数组+静态主席树)

题意:给定一个区间,求这个区间第k大的数,支持单点修改。 思路:主席树真是个神奇的东西.........速度很快但是也有一个问题就是占用内存的很大,一般来说支持单点修改的主席树套树状数组空间复杂度为O...
  • u014664226
  • u014664226
  • 2015年08月21日 20:46
  • 1850
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Quad Trees - ZOJ 1788 四分树
举报原因:
原因补充:

(最多只允许输入30个字)