UVa 297 Quadtrees(四分树)

题意  可以用一个四分图表示一32*32的黑白图像   求两个四分树对应图像相加所得图形黑色部分有多少像素

直接用一个32*32的矩阵表示图  黑色为非0白色为0  递归建图   最后有多少个非零就是答案了

#include<cstdio>
#include<cstring>
using namespace std;
const int L = 32, N = 1050;
char s[N];
int ans[L][L], cnt;

void draw(char *s, int &p, int r, int c, int w)
{
    char ch = s[p++];
    if(ch == 'p')
    {
        draw(s, p, r        , c + w / 2, w / 2);
        draw(s, p, r        , c        , w / 2);
        draw(s, p, r + w / 2, c        , w / 2);
        draw(s, p, r + w / 2, c + w / 2, w / 2);
    }
    else if(ch == 'f')
    {
        for(int i = r; i < r + w; ++i)
            for(int j = c; j < c + w; ++j)
                if(ans[i][j] == 0)
                    ans[i][j] = ++cnt;
    }
}

int main()
{
    int cas;
    scanf("%d", &cas);
    while(cas--)
    {
        memset(ans, 0, sizeof(ans));
        cnt = 0;
        for(int i = 0; i < 2; ++i)
        {
            scanf("%s", s);
            int p = 0;
            draw(s, p, 0, 0, L);
        }
        printf("There are %d black pixels.\n", cnt);
    }
    return 0;
}

 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.

在C#中,我们可以创建一个简单的四叉Quadtrees数据结构。下面是一个基本的四叉类的实现,包括节点添加、划分和查询等操作: ```csharp public class QuadTree<T> where T : IComparable<T> { private Node root; private int width, height; public QuadTree(int width, int height) { this.width = width; this.height = height; root = new Node(null); } // 节点类 private class Node { public T Data { get; set; } public Node[] Children { get; } = new Node[4]; public Node(T data) { Data = data; } public void Split() { if (Children.Any(n => n == null)) { for (int i = 0; i < 4; i++) { if (Children[i] == null) { var splitData = SplitData(Data); Children[i] = new Node(splitData[i]); } } } } // 数据划分策略,这里简单地按中心点将数据分为四个部分 private static T[] SplitData(T data) { return new[] { data / 2, data * 3 / 2, data - data / 2, data + data / 2 }; } } // 添加元素到 public void Insert(T value) { root.Split(); root.Data = value; InsertRecursively(root, value); } // 递归插入函数 private void InsertRecursively(Node node, T value) { if (value.CompareTo(node.Data) < 0) { if (node.Children[0] != null) InsertRecursively(node.Children[0], value); else node.Children[0] = new Node(value); } else if (value.CompareTo(node.Data) > 0) { if (node.Children[2] != null) InsertRecursively(node.Children[2], value); else node.Children[2] = new Node(value); } else return; // 如果值相等,不做处理 // 划分节点 node.Split(); } // 查询节点是否存在特定值 public bool Contains(T value) { return FindNode(root, value) != null; } // 递归查找节点 private Node FindNode(Node node, T value) { if (node == null || node.Data.Equals(value)) return node; for (int i = 0; i < 4; i++) { if (node.Children[i] != null && value.CompareTo(node.Children[i].Data) >= 0) return FindNode(node.Children[i], value); } return null; } } // 示例:创建并使用四叉 QuadTree<int> quadTree = new QuadTree(10, 10); // 宽度和高度设为10 quadTree.Insert(50); bool contains = quadTree.Contains(30); // 返回false,因为30不在范围 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值