297

摘要:
依此类推,直到它最终被划分为最小的1024个像素之一。也就是说,如果每个树都有子节点,它将有四个子树。所以,当我看到四个儿子时,我退出了。总之,只有1024个区块。我将以模拟的形式直接给它们染色。在输入时,使用堆栈的原理来做。只有非叶子可以放在堆栈上,然后我们需要用变量标记子的数量。不过,我在网上看了其他人的代码,用树来做并不是很麻烦。然而,直接做并不方便。
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.

 

297第2张

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.


题意:这道题的意思是有一块图片,总共分为1024块像素。然后每一个大正方形可以分为四块。以此类推,直到最后分为最小的1024块像素之一。

即每棵树如果有子节点,那么就会有4个子树。具体看一下上面的图就一目了然了。


那么这道题我看到四个儿子就退缩了。没有用树来做。

反正也就1024块方块,我就直接用模拟的形式将其染色。在输入的时候,利用栈的原理来做。就是只有非叶子才能入栈,然后要用变量来标记一下这是第几个儿子。

不过我看了网上其他人的代码,用树来做也不是很麻烦。不过做起来还是没有直接染色做的方便吧。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#define maxn 1024
using namespace std;
struct node
{
    char ch;
    int x;
}temp;
char ch;
int t,i,j,lz;
int a1[1200]= {0},a2[1200]= {0};
void input(int *a1)
{
    stack<node> s;
    temp.ch='p';
    temp.x=0;
    int bj=0;
    s.push(temp);
    while(cin>>ch)
    {
        if (ch=='p')
        {
            temp=s.top();
            s.pop();
            temp.x++;
            s.push(temp);
            temp.ch='p';
            temp.x=0;
            s.push(temp);
        }
        else
        {
            temp=s.top();
            lz=maxn/pow(4,(s.size()-1));
            if (ch=='f')
            {
                for (i=bj; i<bj+lz; i++)
                    a1[i]=1;
            }
            bj=bj+lz;//这个变量很重要,每次分块之后,不过染没染色,要移动一个模块。
            s.pop();
            temp.x++;
            s.push(temp);
        }
        temp=s.top();

        while(temp.x==4)
        {
            s.pop();
            temp=s.top();
        }
        //cout<<temp.x<<endl;
        if (s.size()==1) break;
    }
    s.pop();
}
int main ()
{
    char s1[2000],s2[2000];
    cin>>t;
    while(t--)
    {
        memset(a1,0,sizeof(a1));
        memset(a2,0,sizeof(a2));
        input(a1);
        input(a2);
        int sum=0;
        for (i=0; i<maxn; i++)
            if (a1[i] || a2[i])
                sum++;
        printf("There are %d black pixels.
",sum);
    }
    return 0;
}


免责声明:文章转载自《297》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇安卓消息推送解决方案uniapp手写签名下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

redis实现二级缓存

Mybatis也会做缓存,也会有一级缓存和二级缓存:一级缓存:是SqlSession级别的缓存,使用HashMap数据结构来用于存储缓存数据的二级缓存:是mapper级别的缓存,其作用域是mapper的同一个namespace,不同的SqlSession执行两次相同namespace下的sql语句,并且传递的参数相同,返回的结果也相同时,第一次执行sql语句...

canvas基础绘制矩形(1)

1.画布基础知识画布元素是HTML5中添加的一个重要元素,专门用于绘制图形。然而,画布本身不具备绘制图形的能力。将画布元素放置在页面上相当于在页面上放置矩形“画布”。我们可以使用js脚本在“画布”上绘制图形。...

POI操作word和html相互转化

下面是里两个类:第一个类是html转为word,第二个是word转html(最下面附上jar包下载链接)packagecom.wz.poi.wordHtml;/***2018/4/24*@authorAdministrator**/importjava.io.BufferedReader;importjava.io.ByteArrayInputStream;...

故障排查:vsftpd无法用浏览器访问

CentOS6上设置的ftp服务器突然无法使用浏览器访问,但可以使用xftp等工具正常访问。据推测,阿里云的安全组设置之前已经过修改,这可能与1)修改vsftpd的配置,在被动模式下手动指定一个随机连接端口,并添加以下内容:passv_min_port=50000pasv_max_port=60000 02)如果只打开端口20和21,设置阿里云安全组控制端口...

db2字符串函数

可以指定可选的字符串长度单位,以指示哪些单位表示函数的起始位置和结果。使用基于字符的函数解决了将字节位置返回到字符位置的问题。代码单元16和代码单元32根据字符数计数。类似地,CODEUNITS32指定使用Unicode UTF-32来理解多字节字符的字符边界。如果使用CODEUNITS获取字符长度,则用作字符串函数输入的不同CODEUNITS将导致不同的输...

winform中 跨线程启动UI

C#的winform程序中,是不可以从UI窗口主线程之外的线程去直接操作窗口控件的。确切的解释是,不能从创建控件的线程以外的线程去处理控件的操作,比如修改属性等。方法二,通过Control.Invoke调用委托的方式来执行。...