297 - Quadtrees

 Quadtrees 

A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.

Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors. As a result, the quadtree need not be of uniform depth.

A modern computer artist works with black-and-white images of tex2html_wrap_inline34 units, for a total of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.

This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are black in the image, which is the result of adding the two images together.

In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.

Input Specification

The first line of input specifies the number of test cases (N) your program has to process.

The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter 'p' indicates a parent node, the letter 'f' (full) a black quadrant and the letter 'e' (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).

Output Specification

For each test case, print on one line the text 'There are X black pixels.', where X is the number of black pixels in the resulting image.

Example Input

3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe

Example Output

There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.
 
	思路和前面几题二叉树类似,给你一个四叉树先序遍历,每个p节点跟着四个儿子,根据一定是四个儿子节点递归地建树。然后对应点求和,每个黑色节点f对应一个值,一个像素总共是1024,若分成四个分支节点,则加深一层,每个黑色节点对应的值为原来的四分之一。
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

char str[2000];

struct Node
{
  char pixel;
  Node * son[4];
};

typedef Node * Position;

Position buildTree( char * str, int s, int e)   // s为对应树的起始位置,e为树的最后位置
{
    Position root = NULL;
    if( s > e) return root;

    root = new Node;
    root->pixel = str[s];
    for( int i = 0; i < 4; i++) root->son[i] = NULL;

    int cnt = 0;
    for( int i = s+1; i <= e; i++){                                               //建立对应四个儿子
        if( str[i] != 'p') root->son[cnt++] = buildTree( str, i, i);<span style="white-space:pre">		</span>// 如果不是p节点,则对应只有单独一个节点没有后代
        else{
            int k = i;
            for( int t = 0; t < 4; t++, k++) if( str[k+1] == 'p') t-=4;        //若是p节点在该节点后面还有四个该节点的后代,凑齐四个后代,每遇到<span style="white-space:pre">									</span>       //一个p又有四个后代,t减4
            root->son[cnt++] = buildTree( str, i, k);
            i = k;                                                            //下一个后代从k+1开始
        }
    }

    return root;
}
//对两棵树中对应的节点直接求和
int addTree( const Position & t1, const Position & t2, int value)
{
    int ans = 0;

    if( t1->pixel == 'f' || t2->pixel == 'f') return value;             //其中有任意一个是黑色,直接返回黑色节点的值
    if( t1->pixel == 'e' && t2->pixel == 'e') return 0;<span style="white-space:pre">			</span>//都为空,则返回0
//剩下为对应节点为空或为p的情况
    if( t1->pixel == 'e' && t2->pixel == 'p')                            //其中一个为空,则只用算其中一个的值
        for( int i = 0; i < 4; i++)
            ans += addTree( t2->son[i], t2->son[i], value>>2);

    else if( t1->pixel == 'p' && t2->pixel == 'e')
        for( int i = 0; i < 4; i++)
            ans += addTree( t1->son[i], t1->son[i], value>>2);

    else if( t1->pixel == 'p' && t2->pixel == 'p')                 //两个都是p,则求出对应四个儿子(四个子树)的和                    
        for( int i = 0; i < 4; i++)
            ans += addTree( t1->son[i], t2->son[i], value>>2);
         
    return ans;
}



int main()
{
    int n;

    cin >> n;
    while( n--){
     cin >> str;
     Position root1 = buildTree( str, 0, strlen( str)-1);
     cin >> str;
     Position root2 = buildTree( str, 0, strlen( str)-1);

     printf( "There are %d black pixels.\n", addTree( root1, root2, 1024));

    }

    return 0;
}
	还想到另一种方法就是不建树,直接对两个字符串进行计算求和。思路其实是一样的,都是递归的处理。。。。处理当前的树,若有子树则找到子树递归处理
#include <iostream>
#include <string.h>
using namespace std;

char str1[2000], str2[2000];


int getNextSon( char * str, int cur)    //返回树的下一个儿子,cur为当前儿子
{
    int ns = cur+1;
    if( str[cur] != 'p') return ns;    
    else{
        for( int i = 0; i < 4; i++,ns++) //当前儿子cur有后代,还需要多往后移动4位
            if( str[ns] == 'p') i-=4;   //还有后代的话同样移动4位
    }

    return ns;
}

//对两棵树的先序遍历str1和str2直接计算求和,s1和s2分别为两个字符串起始位置(也就是树根),value为当前深度黑色节点对应的值
int getSum( char * str1, int s1, char * str2, int s2, int value) 
{
   if( str1[s1] == 'f' || str2[s2] == 'f') return value; //如果其中一颗树根为黑色节点,直接返回黑色节点的值
   if( str1[s1] == 'e' && str2[s2] == 'e') return 0;   //如果都为空树直接返回0

   int ans = 0;
   int ns1 = s1+1, ns2 = s2+1; //str1和str2里下一个要处理的节点
   
   if( str1[s1] == 'p' && str2[s2] == 'e'){           //另一颗为空树的情况下,直接计算一棵树
        for( int i = 0; i < 4; i++){                  //求四课子树的和
            ans += getSum( str1, ns1, str1, ns1, value>>2);
            ns1 = getNextSon( str1, ns1);
        }
   }
   else
   if( str2[s2] == 'p' && str1[s1] == 'e'){           //同上面的情况
        for( int i = 0; i < 4; i++){
            ans += getSum( str2, ns2, str2, ns2, value>>2);
            ns2 = getNextSon( str2, ns2);
        }
   }
   else
   if( str1[s1] == 'p' && str2[s2] == 'p'){       //两棵树都有子树,计算四个对应子树的和
        for( int i = 0; i < 4; i++){
            ans += getSum( str1, ns1, str2, ns2, value>>2);
            ns1 = getNextSon( str1, ns1);
            ns2 = getNextSon( str2, ns2);
        }
   }

   return ans;
}

int main()
{
    int n;
    cin >> n;
    while( n--){
        cin >> str1 >> str2;
        cout << "There are "<< getSum( str1, 0, str2, 0, 1024) << " black pixels.\n";
        ;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值