UVA - 297 Quadtrees(四分图)

本文介绍了一种基于四叉树表示的图像合并算法,该算法用于计算两个图像相加后形成的新的图像中黑色像素的数量。四叉树是一种用于编码图像的表示形式,通过将图像分解为四个象限并递归地进行细分来表示图像的不同颜色区域。文章详细解释了四叉树的结构和如何使用预定义的字符串表示四叉树,以及如何根据这些表示来计算合并后的图像中的黑色像素总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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  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表示黑色,e表示白色。

注:题中给出树最多5层

思路:根据递归建树,然后将最下面的黑色节点标记,最后循环统计,(很暴力)。

#include<stdio.h>
#include<string.h>
char s[5400];
int book[1400],k;
int judge(int x)
{
    if(x>1364)
        return 1;
    for(int i=x; i<x+4; i++)//同父的四个节点
    {
        if(judge(4*i+1))//判断为最后一层的节点然后标记
        {
            book[i]=1;
        }
    }
    return 0;
}
void solve(int x)//编号
{
    if(s[k]=='p')//p为中间节点,需要跳过向后遍历
    {
        k++;
        solve(x*4+1);
        solve(x*4+2);
        solve(x*4+3);
        solve(x*4+4);
        return ;
    }
    else if(s[k]=='f')
    {
        if(judge(4*x+1)&&x<=1364)//f为它的子节点都为黑(向下递归)
            book[x]=1;
    }
    k++;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int cnt=0;
        memset(book,0,sizeof(book));//book数组标记
        memset(s,'\0',sizeof(s));
        scanf("%s",s);
        k=0;
        solve(0);//从0根节点开始

        scanf("%s",s);
        k=0;
        solve(0);//
        for(int i=0;i<=1364;i++)//一共1364个节点
        {
            if(book[i]==1)
                cnt++;
        }
        printf("There are %d black pixels.\n",cnt);
    }
    return 0;
}

 

 

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值