uva 297 Quadtrees

点击打开链接

题目意思:给定两个字符串,字符p对应建立子树,字符e为白色,f为黑色对于不同层黑点f对应不同的值,最上面为1024下来为每个子树256.....,这样建立两颗四叉树,计算两颗树相加后的黑点f对应的值,注意在两颗树上同一点处,只要有一个为黑点,那么相加后就为黑

解题思路:1首先建立两颗树,采用递归建树的方法  2建完树后利用前序遍历的方法进行递归求解和

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <deque>
#include <algorithm>
using namespace std;

const int MAXN = 1000010;//定义一个常量

//4叉树的结构体
struct Btree{
    char c;
    struct Btree *child[4];//四叉我们开个数组存储四个子树
};
Btree *root1 , *root2;//两颗树的根节点
char  str1[MAXN] , str2[MAXN];//存储读入的两串字符串
int l1 , l2 , sum , pos;

//4叉树的初始华
void newnode(Btree *u){
    u -> c = 0;
    for(int i = 0 ; i < 4 ; i++)
        u -> child[i] = NULL;
}

//树的递归建立
Btree* BuildTree(Btree *root , char *str){//返回Btree*
    ++pos;//pos要为全局,注意递归的全局和局部变量区别
    if(pos == strlen(str))//如果达到字符串末尾则返回NULL
        return NULL;
    root = new Btree;//每一New一个
    newnode(root);//new出来要习惯初始化
    root -> c = str[pos];//根节点的值为str[pos]
    if(str[pos] == 'p'){//如果为p则建立子树,否则直接返回根节点就好
        for(int i = 0 ; i < 4 ; ++i){
            if(root->child[i] == NULL){//如果哪个子树为空则递归产生子树
                root->child[i] = BuildTree(root->child[i] , str);
            }
        }
    }
    return root;
}

//前序求和(同时遍历两颗树)
void preorder(Btree *root1 , Btree *root2 , int level){
    if(root1 == NULL && root2 == NULL)//如果两颗树此时都为空则直接返回
        return;
    if(root1 == NULL){//树1为空时候
        if(root2->c == 'f'){//如果当前树2的值为f要加上值1024>>(level*2)
            sum += 1024>>(level*2);//位运算的使用
            return;//为f   说明子树为空直接返回即可
        }
        for(int i = 0 ; i < 4 ; i++)//否则遍历子树2的四个方向
            preorder(root1 , root2->child[i] , level+1);
        return;//记得每一次都要返回
    }
    if(root2 == NULL){//同上
        if(root1->c == 'f'){
            sum += 1024>>(level*2);
            return;
        }
        for(int i = 0 ; i < 4 ; i++)
            preorder(root1->child[i] , root2 , level+1);
        return;
    }
    //如果此时要个都不为空只要有一个是f就要加上对应值
    if(root1->c == 'f' || root2->c == 'f'){
        sum += 1024>>(level*2);
        return;//返回上一层
    }
    for(int i = 0 ; i < 4 ; i++)//四个方向的遍历
        preorder(root1->child[i] , root2->child[i] , level+1);
}

//输入函数
void solve(){
    pos = -1;
    root1 = new Btree;
    root2 = new Btree;
    newnode(root1);//初始化
    newnode(root2);
    root1 = BuildTree(root1 , str1);//建树
    root2 = BuildTree(root2 , str2);//建树
    sum = 0;
    preorder(root1 , root2 , 0);
    printf("There are %d black pixels.\n" , sum);
}
//主函数
int main(){
    int t;
    scanf("%d" , &t);
    while(t--){
        scanf("%s%s" , str1 , str2);//输入两串字符串
        solve();
    }
    return  0;
}
时间: 2024-07-30 23:16:32

uva 297 Quadtrees的相关文章

UVa 297:Quadtrees 四叉树及其在编码图像的应用

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=233 题目类型: 数据结构, 二叉树 题意与背景: 同二叉树一样,四叉树也是一种数据结构,是一种每个节点最多有四个子树的数据结构. 一个四叉树是可以表示格式用于编码图像.背后的基本思想是,  任何图像可以分为四个象限,每个象限也可以再次被分割成4个亚象限,等等.因此四叉树是在二维图

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic