UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS)
题意分析
将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块。字符表示规则是:
p表示这个像素块继续分解,e表示当前方格没有像素,即为空,f表示当前像素块为满,黑色。
最后求解两个数合并后的像素块的数量是多少。
最大的像素块数量是1024个。
采用数组模拟,根据所给的字符串,递归建树。字符数组的建四分树的技巧是(k << 2) + i i∈[-2,1]。 这样就可以充分利用数组的空间。
两树合并的技巧,对于某一节点,如果有一个节点的值为0,另一个对应节点的值为非0,那么这个节点合并后的值就为非0值。 或者是当前节点的像素块为此深度所能到达的最大值,那么合并后即为这个深度的最大值。否则的话,继续向下递归合并。
最后输出合并后的结果即可。
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define nmax 1200
#define standerd 1024
#define hh standerd / (1<<((depth-1)*2))
#define INF 0x3f3f3f3f
using namespace std;
int ta[nmax<<2],tb[nmax<<2];
char str[10000];
int pos = 0;
void pushup(int rt, int t[])
{
int sum = 0;
for(int i = -2; i<=1; ++i)
sum+=t[(rt<<2)+i];
t[rt] = sum;
}
void dfs(int t[],int rt,int depth)
{
if(str[pos] == 'e') {
return;
}
if(str[pos] == 'f') {
t[rt] = hh;
}
if(str[pos] == 'p'){
for(int i = -2; i<=1; ++i){
pos++;
dfs(t,(rt<<2)+i,depth+1);
pushup(rt,t);
}
}
}
bool judge(int depth, int rt)
{
if(ta[rt] == hh || tb[rt] == hh) return true;
else return false;
}
int cal(int t1[], int t2[],int rt,int depth, int sum)
{
if((!t1[rt] || !t2[rt])|| judge(depth,rt))
sum = max(t1[rt],t2[rt]);
else
for(int i = -2; i<=1; ++i)
sum += cal(t1,t2,(rt<<2)+i,depth+1,0);
return sum;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
memset(ta,0,sizeof(ta));
memset(tb,0,sizeof(tb));
pos = 0;
scanf("%s",str);
dfs(ta,1,1);
pos = 0;
scanf("%s",str);
dfs(tb,1,1);
int sum = cal(ta,tb,1,1,0);
printf("There are %d black pixels.\n",sum);
}
return 0;
}
UVA.297 Quadtrees (四分树 DFS)的更多相关文章
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- uva 297 quadtrees——yhx
Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind ...
- UVa 297 - Quadtrees
题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...
- UVa 297 Quadtrees -SilverN
A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...
- Quadtrees(四分树)
uva 297 Quadtrees Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Subm ...
- 四分树 (Quadtrees UVA - 297)
题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...
- UVa 297 (四分树 递归) Quadtrees
题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...
随机推荐
- 代码重复率检查工具jsinspect
检查重复代码,去掉冗余代码. 安装: npm install -g jsinspect 用法:jsinspect [options] <paths ...> 检测复制粘贴和结构类似的Jav ...
- 安装MySQLdb模块遭遇"fatal error: my_config.h: No such file or directory"的处理
Issue I encountered an error when I run the python script which need to import the module of & ...
- InnoDB锁冲突案例演示(续)
Preface I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...
- Linux命令应用大词典-第7章 字符串、文件和命令查找
7.1 grep:字符串.文件和命令的查找 7.2 egrep:在文件或标准输入中查找模式 7.3 fgrep:在每个文件或是标准输入中查找模式 7.4 find:列出文件系统内符合条件的文件 7.5 ...
- 一步一步图文介绍SpriteKit使用TexturePacker导出的纹理集Altas
1.为什么要使用纹理集? 游戏是一种很耗费资源的应用,特别是在移动设备中的游戏,性能优化是非常重要的 纹理集是将多张小图合成一张大图,使用纹理集有以下优点: 1.减少内存占用,减少磁盘占用: 2.减少 ...
- Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet
第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...
- Python爬虫使用浏览器的cookies:browsercookie
很多用Python的人可能都写过网络爬虫,自动化获取网络数据确实是一件令人愉悦的事情,而Python很好的帮助我们达到这种愉悦.然而,爬虫经常要碰到各种登录.验证的阻挠,让人灰心丧气(网站:天天碰到各 ...
- 【WXS全局对象】Number
属性: 名称 说明 Number.MAX_VALUE 返回JS中可表示的最大的数.它的近似值为 1.7976931348623157 x 10308. Number.MIN_VALUE 返回JS中可表 ...
- 1053 Path of Equal Weight (30 分)(树的遍历)
题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径 #include<bits/stdc++.h> using namesp ...
- 使用Response.Write实现在页面的生命周期中前后台的交互
Response.Write()方法非常的常见,也很普通,就是向http output中输出一string.其输出的内容位于页面的最顶端,常用来实现显示一些页面消息框等逻辑. 一般来说,在页面的整个生 ...