zoj 1788 Quad Trees
先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归
四分树建立完后,再进行一深度优先遍历,生成二进制字符串,再转化为16进制输出
//#include "stdafx.h"
#include <string.h>
#include <string>
#include <queue>
#include <iostream>
#include "stdio.h"
using namespace std;
char MAP[512][512];
class quadtree//结点
{
public:
char value[3];// "00"->all 0 ,"01"->all 1,"1"->mixed
quadtree*q[4];//four children
quadtree()
{
q[0] = q[1] = q[2] = q[3] = 0;//initialize four children
}
bool operator == (const quadtree &p)const//运算符重载,
{
if (strcmp(value, "1") == 0 || strcmp(value, p.value) != 0)
return 0;
else
return 1;
} };
quadtree * DFS(int r, int c, int s)//r ,c 坐标,s长度
{
int i; bool f = 1;
quadtree*temp = new quadtree;
if (s==1)//最小长度,每一个格
{
temp->value[0] = '0';
temp->value[1] = MAP[r][c];
temp->value[2] = 0;//串结束符号?
return temp; }
s /= 2;//四分
temp->q[0] = DFS(r, c, s);
temp->q[1] = DFS(r, c + s, s);
temp->q[2] = DFS(r + s, c, s);
temp->q[3] = DFS(r + s, c + s, s);
for (i = 1; i < 4;i++)
{
if(!(*temp->q[0] == *temp->q[i]))//some of four children are different ,can not be merged
{
f = 0;
break;
}
}
if (f)//all children are same,merge
{
strcpy(temp->value, temp->q[0]->value);
for (i = 0; i < 4; i++)
{
delete temp->q[i]; temp->q[i] = 0;//delete all children
}
}
else// don't merge
{
strcpy(temp->value, "1");
}
return temp;
}
string BFS(quadtree*root)//广度遍历,生成二进制字符串
{
string s = "";
quadtree *cur = root;
queue <quadtree*> que;
que.push(root);
while (!que.empty())
{
cur = que.front();
que.pop();
s += cur->value;
for (int i = 0; i < 4; i++)
{
if (cur->q[i]->value != NULL)//有子才放进去!!!!!!!
que.push(cur->q[i]);
}
}
return s;
}
//////////////////////////////////////////////
//转化为16进制。。别人家的代码
char tohex(const string& str)
{
int ret = 0; for (int i = 0; i < 4; i++)
{
ret <<= 1;//左移 实现16进制的转化
if (str[i] == '1')
++ret;
} return (ret < 10) ? '0' + ret : 'A' + ret - 10;
}
string ToHex(const string& str)
{
string tmp = str;
string ret = ""; while (tmp.length() % 4 != 0)
tmp = "0" + tmp;
for (size_t i = 0; i < tmp.length(); i += 4)
ret += tohex(tmp.substr(i, 4));//substr 截取指定字符串,i为起始位置,4表示长度 return ret;
}
////这部分是别人家的 = =
///////////////////////////////////////////////////
int main()
{
string al;
int k, N;
scanf("%d", &k);//输入test 次数
while (k--)
{
scanf("%d", &N);
for (int i = 0; i < N; i++)//输入矩阵大小
{
for (int j = 0; j < N; j++)
{
cin >> MAP[i][j];// scanf("%c", &MAP[i][j]);//initialize the array } }
quadtree *root;
root =DFS(0, 0, N);//build a quardtree;
al = BFS(root);
//cout << al << endl;
cout << ToHex(al) << endl;
}
return 0;
}
zoj 1788 Quad Trees的更多相关文章
- [LeetCode] Quad Tree Intersection 四叉树相交
A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight, ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】558. Quad Tree Intersection 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】427. Construct Quad Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- “等一下,我碰!”——常见的2D碰撞检测
转自:https://aotu.io/notes/2017/02/16/2d-collision-detection/ 在 2D 环境下,常见的碰撞检测方法如下: 外接图形判别法 轴对称包围盒(Axi ...
- elasticsearch 口水篇(6) Mapping 定义索引
前面我们感觉ES就想是一个nosql数据库,支持Free Schema. 接触过Lucene.solr的同学这时可能会思考一个问题——怎么定义document中的field?store.index.a ...
随机推荐
- 在DOM中搜索元素
方法 现代浏览器中使用XPath document.getElementById document/node.getElementsByTagName Limit search by parent e ...
- data([key],[value])
概述 在元素上存放或读取数据,返回jQuery对象. 当参数只有一个key的时候,为读取该jQuery对象对应DOM中存储的key对应的值,值得注意的是,如果浏览器支持HTML5,同样可以读取该DOM ...
- Breeze库API总结(Spark线性代数库)(转载)
导入 import breeze.linalg._ import breeze.numerics._ Spark Mllib底层的向量.矩阵运算使用了Breeze库,Breeze库提供了Vector/ ...
- [OSG]OSG例子程序简介
1.example_osganimate一)演示了路径动画的使用(AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera.CameraView.M ...
- 我的git与github学习历程
因为想要知道如何把代码放到github上,所以就百度了一下,然后找到一个<如何从github上面拷贝源码>的文章,就先进行练习了下 1.首先到git官网下载git版本控制工具的安装包, ...
- PHP Problem with the SSL CA cert (path? access rights?)
1.php使用curl模块报错问题 开发遇到问题,直接使用系统的curl命令正常,使用php的curl模块报错 错误:PHP Problem with the SSL CA cert (path? a ...
- usr类库的使用(一般用在第三方类库使用系统库报错头文件找不到时)
第三方Html解析类库Hpple,在导入框架libxml2.2.dylib后,XCode仍然找不到<libxml/tree.h>. 1 .项目 -Targets 中的 Build P ha ...
- java split进行字符串分割
在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用"."作为分隔的话,必须是如下写法,Str ...
- 【前端】js中new和Object.create()的区别
js中new和Object.create()的区别 var Parent = function (id) { this.id = id this.classname = 'Parent' } Pare ...
- No module named 'urllib2'
import urllib2 response = urllib2.urlopen('http://www.baidu.com/') html = response.read() print html ...