zoj 1788

先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归

四分树建立完后,再进行一深度优先遍历,生成二进制字符串,再转化为16进制输出

  1. //#include "stdafx.h"
  2. #include <string.h>
  3. #include <string>
  4. #include <queue>
  5. #include <iostream>
  6. #include "stdio.h"
  7. using namespace std;
  8. char MAP[512][512];
  9. class quadtree//结点
  10. {
  11. public:
  12. char value[3];// "00"->all 0 ,"01"->all 1,"1"->mixed
  13. quadtree*q[4];//four children
  14. quadtree()
  15. {
  16. q[0] = q[1] = q[2] = q[3] = 0;//initialize four children
  17. }
  18. bool operator == (const quadtree &p)const//运算符重载,
  19. {
  20. if (strcmp(value, "1") == 0 || strcmp(value, p.value) != 0)
  21. return 0;
  22. else
  23. return 1;
  24. }
  25.  
  26. };
  27. quadtree * DFS(int r, int c, int s)//r ,c 坐标,s长度
  28. {
  29. int i; bool f = 1;
  30. quadtree*temp = new quadtree;
  31. if (s==1)//最小长度,每一个格
  32. {
  33. temp->value[0] = '0';
  34. temp->value[1] = MAP[r][c];
  35. temp->value[2] = 0;//串结束符号?
  36. return temp;
  37.  
  38. }
  39. s /= 2;//四分
  40. temp->q[0] = DFS(r, c, s);
  41. temp->q[1] = DFS(r, c + s, s);
  42. temp->q[2] = DFS(r + s, c, s);
  43. temp->q[3] = DFS(r + s, c + s, s);
  44. for (i = 1; i < 4;i++)
  45. {
  46. if(!(*temp->q[0] == *temp->q[i]))//some of four children are different ,can not be merged
  47. {
  48. f = 0;
  49. break;
  50. }
  51. }
  52. if (f)//all children are same,merge
  53. {
  54. strcpy(temp->value, temp->q[0]->value);
  55. for (i = 0; i < 4; i++)
  56. {
  57. delete temp->q[i]; temp->q[i] = 0;//delete all children
  58. }
  59. }
  60. else// don't merge
  61. {
  62. strcpy(temp->value, "1");
  63. }
  64. return temp;
  65. }
  66. string BFS(quadtree*root)//广度遍历,生成二进制字符串
  67. {
  68. string s = "";
  69. quadtree *cur = root;
  70. queue <quadtree*> que;
  71. que.push(root);
  72. while (!que.empty())
  73. {
  74. cur = que.front();
  75. que.pop();
  76. s += cur->value;
  77. for (int i = 0; i < 4; i++)
  78. {
  79. if (cur->q[i]->value != NULL)//有子才放进去!!!!!!!
  80. que.push(cur->q[i]);
  81. }
  82. }
  83. return s;
  84. }
  85. //////////////////////////////////////////////
  86. //转化为16进制。。别人家的代码
  87. char tohex(const string& str)
  88. {
  89. int ret = 0;
  90.  
  91. for (int i = 0; i < 4; i++)
  92. {
  93. ret <<= 1;//左移 实现16进制的转化
  94. if (str[i] == '1')
  95. ++ret;
  96. }
  97.  
  98. return (ret < 10) ? '0' + ret : 'A' + ret - 10;
  99. }
  100. string ToHex(const string& str)
  101. {
  102. string tmp = str;
  103. string ret = "";
  104.  
  105. while (tmp.length() % 4 != 0)
  106. tmp = "0" + tmp;
  107. for (size_t i = 0; i < tmp.length(); i += 4)
  108. ret += tohex(tmp.substr(i, 4));//substr 截取指定字符串,i为起始位置,4表示长度
  109.  
  110. return ret;
  111. }
  112. ////这部分是别人家的 = =
  113. ///////////////////////////////////////////////////
  114. int main()
  115. {
  116. string al;
  117. int k, N;
  118. scanf("%d", &k);//输入test 次数
  119. while (k--)
  120. {
  121. scanf("%d", &N);
  122. for (int i = 0; i < N; i++)//输入矩阵大小
  123. {
  124. for (int j = 0; j < N; j++)
  125. {
  126. cin >> MAP[i][j];// scanf("%c", &MAP[i][j]);//initialize the array
  127.  
  128. }
  129.  
  130. }
  131. quadtree *root;
  132. root =DFS(0, 0, N);//build a quardtree;
  133. al = BFS(root);
  134. //cout << al << endl;
  135. cout << ToHex(al) << endl;
  136. }
  137. return 0;
  138. }

zoj 1788 Quad Trees的更多相关文章

  1. [LeetCode] Quad Tree Intersection 四叉树相交

    A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight,  ...

  2. [LeetCode] Construct Quad Tree 建立四叉树

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  3. LeetCode 427 Construct Quad Tree 解题报告

    题目要求 We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be tru ...

  4. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  5. leetcode 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  6. 【LeetCode】558. Quad Tree Intersection 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】427. Construct Quad Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. “等一下,我碰!”——常见的2D碰撞检测

    转自:https://aotu.io/notes/2017/02/16/2d-collision-detection/ 在 2D 环境下,常见的碰撞检测方法如下: 外接图形判别法 轴对称包围盒(Axi ...

  9. elasticsearch 口水篇(6) Mapping 定义索引

    前面我们感觉ES就想是一个nosql数据库,支持Free Schema. 接触过Lucene.solr的同学这时可能会思考一个问题——怎么定义document中的field?store.index.a ...

随机推荐

  1. python学习笔记之基础一(第一天)

    1. python字符介绍 在C语言中没有字符串,只有字符 在python中的字符串hello,在C语言中是以字符数组在内存存放['h','e','l','l','o'],如果对字符串修改,则是在内存 ...

  2. linux 时间管理——概念、注意点(一)【转】

    转自:http://www.cnblogs.com/openix/p/3324243.html 参考:1.http://bbs.eyeler.com/thread-69-1-1.html        ...

  3. jquery 获取元素背景图片backgroungImage的url

    $("#").css("backgroundImage").replace('url(','').replace(')','');

  4. mysql connection refused

    mysql数据库认证的时候和别的服务器不一样,即使mysqld数据库服务器没有启动,使用mysql这种客户端程序去连接,也要先输入密码,从而使人有一种错觉,以会服务器已经正常启动了.是不是密码或是主机 ...

  5. CSS3简单的小技巧:linear-gradient切角画册

    关于linear-gradient的语法就不多做介绍了网上到处都是,下面看个小例 我们先做一个渐变,使其让他旋转, <div class="example"> < ...

  6. PB的datawindow导出到excel文件(使用saveasascii)

    **********************************************************//*函数名称:uf_dwsaveas_excel功能:将数据窗口数据导出EXCEL ...

  7. boldSystemFontOfSize 和 systemFontOfSize 的区别

    使用 UIFont 的下列方法: + systemFontOfSize + boldSystemFontOfSize + italicSystemFontOfSize p.p1 { margin: 0 ...

  8. 3d游戏模型及地形提取及导航

    支持所有DirectX的游戏模型提取 有需要的可以直接联系我!QQ290387340

  9. SpringMVC生成Excel下载

    SpringMVC controller里的方法: @RequestMapping(value="/notify/download",produces = {"appli ...

  10. 正确使用ng-if和ng-show

    在使用bootstrap中,我们会经常用到按钮组,也就是btn-group,如果仔细观察的话,会发现一个按钮组的第一个和最后一个按钮分别是有圆角的,如下图: 但是中间的按钮是没有圆角的,这样显得比较美 ...