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)的更多相关文章

  1. UVa 297 Quadtrees(树的递归)

    Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...

  2. UVA - 297 Quadtrees (四分树)

    题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...

  3. uva 297 quadtrees——yhx

    Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind ...

  4. UVa 297 - Quadtrees

    题目:利用四叉树处理图片,给你两张黑白图片的四叉树,问两张图片叠加后黑色的面积. 分析:搜索.数据结构.把图片分成1024块1*1的小正方形,建立一位数组记录对应小正方形的颜色. 利用递归根据字符串, ...

  5. UVA 297 Quadtrees(四叉树建树、合并与遍历)

    <span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: r ...

  6. UVa 297 Quadtrees -SilverN

    A quadtree is a representation format used to encode images. The fundamental idea behind the quadtre ...

  7. Quadtrees(四分树)

    uva 297 Quadtrees Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Subm ...

  8. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

  9. UVa 297 (四分树 递归) Quadtrees

    题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...

随机推荐

  1. Ubuntu 14.04 登录 界面添加 root账号

    1打开终端输入:sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 2在弹出的编辑框里加入:greeter-show-manual- ...

  2. 如何在Mac OS系统下搭建Java开发环境 配置Java环境变量

    1. 打开终端   在finder里面搜索(这台MAC)查找终端   2. 在cdlouiedeAir:~ cdlouie$(cdlouie是我的电脑用户名) 后面输入sudo vim /etc/pr ...

  3. 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  4. ant-design学习准备_1

    在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...

  5. BehaviorDesigner学习

    行为树: 行为树设计师插件是一个专门为unity设计的AI插件. 学习用!!!插件地址:链接:http://pan.baidu.com/s/1dF2okPN 密码:b43m 通过继承Behavior中 ...

  6. OIDC in Angular 6

    参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/i07702h18nz.html 1. OIDC-Client https://githu ...

  7. python学习摘要(3)--字符串处理函数

    python没有字符类型, "字符串" '字符串' '''字符串''' """字符串""" 三引号可以支持字符串跨行 字 ...

  8. python-网易云简单爬虫

    一.准备工作 1.使用python3.6和pycharm 2.使用的模块 tkinter .requests .beautifulSoup.getpass.os 3.网易云的榜单页面地址 https: ...

  9. Spring Boot(七)扩展分析

    前面的章节在分析SpringBoot启动过程中,我们发现SpringBoot使用Spring框架提供的SpringFactoriesLoader这个类,实现检索META-INF/spring.fact ...

  10. PAT 甲级 1128 N Queens Puzzle

    https://pintia.cn/problem-sets/994805342720868352/problems/994805348915855360 The "eight queens ...