Quad Trees

题目传送门~

Time Limit: 2000 msMemory 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/2N/2 array does not have the same binary value, such as the upper right and lower right N/2N/2 arrays in Figure 2(c), then we can divide it into four N/4N/4 arrays again. These N/4N/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


题后两句话:

①啊啊啊啊啊这个四分树是真的冷门而且不简单,理解还挺好理解的,就是要考虑的细节超级多哭辽。

②顺一波题意康康:输入一个整数num:1 ~100,表示测试的组数,然后每组测试数据先输入一个整数N,N <= 512且N是2的次幂,然后输入一个N*N的0,1矩阵。如果整个N*N的矩阵是全0(或者全1),则记录为00(或者01),不用继续拆分;否则,记录为1,并将矩阵四分为西北、东北、西南、东南各一块,并按此顺序检查各个分块是否全0或全1, 不是的话继续四分,最后把所有的01串连起来,转换为十六进制输出即可。

③可能很随便的一想就感觉先检查,然后四分,再检查再四分吧啦吧啦这样下去,但是如果某组矩阵维度过高则会使检查的时间开销大大增加,可能就TLE辽嘤嘤嘤嘤...所以我的思路是先把整个矩阵四分到不可再分,再逐一检查每个分叉,进行合并剪枝的操作,这样就避免了大面积检查浪费时间的情况。

④然后就是要把四分树中的0,1数字串用层次遍历的方法读取出来,再以四位为一个单位计算出十进制数并转化成十六进制输出。

⑤归结起来整体思路就是:

(1)深度优先生成四分树  (2)层次遍历四分树得到二进制数列  (3)将二进制数列转化为十六进制输出

⑥学到一个tip就是怎么输出八或者十六进制的数(详情看代码)


废话不多说了,上才艺!!!

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;class quadtree{  //四分树类 public:char value[3];  //取值"00","01","1"(其中00表示全0, 01表示全1, 1表示mixed quadtree *child[4];quadtree(){child[0]=child[1]=child[2]=child[3]=0;} bool operator ==(const quadtree& p) const{  //运算符重载 if(strcmp(value,"1")==0||strcmp(value,p.value)!=0) return 0;  //如果有孩子值为1或者存在相异值则不能合并 else return 1;}}; quadtree *head;
char map[520][520],ans[10000],str[5];
int N,a[2500];//组织输入并进行初始化
void init(){  scanf("%d",&N);for(int i=0;i<N;i++)for(int j=0;j<N;j++)cin>>map[i][j];str[4]=0;memset(ans,0,sizeof(ans));
} //深度优先之四分树的四分,合并,剪枝
quadtree * DFS(int r, int c, int len)//r ,c 坐标,len长度
{quadtree*temp = new quadtree;if(len==1){  //到四分到最小格时停止 temp->value[0]='0';  //第一个数 置0 temp->value[1]=map[r][c];  //第二个数则为方格中的数字 temp->value[2]=0;  //最后一位 置0 表示结束符 return temp;}len/=2;  //将map四分 temp->child[0]=DFS(r,c,len);temp->child[1]=DFS(r,c+len,len);temp->child[2]=DFS(r+len,c,len);temp->child[3]=DFS(r+len,c+len,len);bool flag=true;for(int i=1;i<4;i++)  //判断是否符合合并要求 if(!(*temp->child[0]==*temp->child[i])){ flag=false;break;}if(flag)  //若满足要求则合并 {strcpy(temp->value, temp->child[0]->value);for (int i = 0; i < 4; i++){delete temp->child[i];  //剪枝 temp->child[i]=0;}}else strcpy(temp->value, "1");  //否则该节点不合并,并且节点值为1 return temp;
}//将二进制转化为十进制数
void funtion(char s[]){int sum=0;for(int i=0;i<4;i++) sum=sum*2+str[i];printf("%X",sum);
}//层次遍历取出二进制数字字符串,并转为十六进制输出
void print(){int i,j,m;quadtree *temp;queue<quadtree *> q;q.push(head);while(!q.empty()){temp=q.front();q.pop();strcat(ans,temp->value);  //将二进制串连接起来if(!(temp->child[0]==0))  //若该节点存在孩子for(i=0;i<4;i++) q.push(temp->child[i]);delete temp; }int slen =strlen(ans);  //计算ans的长度 int pos=0;  //标记ans当前计算的位置 //为了方便计算,将整个数字串长度补成4的倍数//用长度求余数得i,在最前面补4-i个0if(slen%4!=0){i=slen%4;for(j=0;j<4-i;j++) str[j]=0;for(m=j;m<4;m++) str[m]=ans[pos++]-'0'; funtion(str);}for(i=pos;i<slen;i+=4){for(j=0;j<4;j++) str[j]=ans[i+j]-'0';funtion(str);}printf("\n");
} int main(){int num;scanf("%d",&num);while(num--){init();head=DFS(0,0,N);print();}return 0;
}

ZOJ 1788 Quad Trees (四分树经典)相关推荐

  1. 非二叉树 UVA297 四分树 Quadtrees

    UVA297 四分树 Quadtrees 题意翻译 如图所示,可以用四分图来表示一个黑白图像,方法是用根节点表示整幅图像,然后把行列个分成两等份,按图中的方式编号,从左到右对应4个子节点.如果某子节点 ...

  2. 四分树(UVa297紫书p160)

    题目描述 输入格式 输出格式 题意翻译 如图所示,可以用四分图来表示一个黑白图像,方法是用根节点表示整幅图像,然后把行列个分成两等份,按图中的方式编号,从左到右对应4个子节点.如果某子节点对应的区域全 ...

  3. 【COGS】1577 [OIBH 练习赛#6]战地统计系统 四分树

    传送门:[COGS]1577 [OIBH 练习赛#6]战地统计系统 题目分析:赤果果的四分树哦~,题目分析就不说了,大家看到一定都会,但是我要吐嘈一下数据= =,这题目的数据竟然有一组是错误的!x1 ...

  4. Ternary Search Trees 三分树

    Efficient auto-complete with a ternary search tree 分类: 算法和数据结构学习 2012-04-18 18:03  125人阅读  评论(0)  收藏 ...

  5. 主席树经典应用区间合并

    线段树经典应用2 参考代码: https://vjudge.net/problem/CodeForces-484E#include<iostream> #include<algori ...

  6. LOJ#3159. 「NOI2019」弹跳(四分树+dijkstra)

    传送门 n2n^2n2暴力显然,考虑优化. 有一种想法是使用四分树/kd-tree/树套树,发现你并不能得到100pts100pts100pts的好成绩(空间会炸掉) 考试的时候比较智熄,先暴力四分树 ...

  7. zoj - 2112 带修改主席树 + 空间优化

    ZOJ - 2112 题意:求区间第k小 思路:带修改区间第k小裸题,无修改的主席树是维护一个前缀线段树,每次更新log个节点,用root 和 ls rs作为每颗前缀线段树的根节点和左右子树的索引(相 ...

  8. zoj 3511 Cake Robbery(线段树)

    题目链接:zoj 3511 Cake Robbery 题目大意:就是有一个N边形的蛋糕.切M刀,从中挑选一块边数最多的.保证没有两条边重叠. 解题思路:有多少个顶点即为有多少条边,所以直接依照切刀切掉 ...

  9. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  10. 线段树 (经典题目合集)

    目录 1.子段乘积 P3372 [模板]线段树 1(加法线段树) P3373 [模板]线段树 2(乘法线段树) P4145 上帝造题的七分钟2 / 花神游历各国(根号线段树) 1.子段乘积 直接暴力做 ...

最新文章

  1. OSChina 周三乱弹 —— 一起 High High High!
  2. python将字典作为参数传入函数
  3. python导入模块有几种方式、各有什么特点_Python导入模块的几种姿势
  4. EMLOG SSL插件 一键开启/关闭ssl无需操作数据库
  5. ERC721藏品合约详解,附代码实现
  6. 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column
  7. 2021水电消纳交易电量增长超150% 价格已提高16%
  8. zjoi 2008 树的统计——树链剖分
  9. Activity调度机制
  10. 真解决EasyUi的 select 使用 class=“easyui-combobox“ 样式绑定onSelect/onChange事件
  11. MATLAB中.m文件命名规则
  12. 计算机原理处理器,多处理器结构-微计算机原理-电子发烧友网站
  13. 关于阿里巴巴icon矢量图显示空白问题
  14. 如何解决wup.exe文件占用cpu资源
  15. Scene…… couldn‘t be loaded because it has not been added to the build settings or the AssetBundle...
  16. 视频剪辑,教你给每个视频画面上添加透明图片
  17. 华中师大计算机专业陈鹏,考完就放假!这套华师真题,你能拿多少分
  18. ES6学习笔记:箭头函数
  19. python的价值观_Python beauthulsoup刮刮雅虎财经价值观
  20. 关于hash哈希以及为什么python中dict和set的key必须为不可变对象

热门文章

  1. Win10安装Apache和PHP
  2. AR涂涂乐项目之识别图制作制作地球仪线框一
  3. LwIP+ STM32+HTTP例程参考
  4. 服务器异常原因和解决方法
  5. strick-footer 粘边布局
  6. 剑指offer-动态规划入门篇
  7. html网页什么样的字体最好看,css设置各种中文字体样式代码
  8. jquery fullpage
  9. C语言趣味题:猜数字游戏(含代码创建思路与过程)
  10. 局域网稳定性测试软件,局域网速度测试