四分树(Quadtrees)

2023-05-16

Quadtrees
Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

Submit  Status

Description

Download as PDF

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++语言编写程序,代码如下:

#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 1024 + 10;
char s[maxn];
//string s;
const int len = 32;
int buf[len][len], cnt;

//把字符串s[p..]导出到以(r, c)为左上角,边长为w的缓冲区中
//2 1
//3 4
void draw(const 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);              //1
		draw(s, p, r, c, w / 2);                           //2
		draw(s, p, r + w / 2, c, w / 2);              //3
		draw(s, p, r + w / 2, c + w / 2, w / 2); //4
	}
	else if (ch == 'f') { //画黑像素(白像素不画)
		for (int i = r; i < r + w; i++)
			for (int j = c; j < c + w; j++)
				if (buf[i][j] == 0) {
					buf[i][j] = 1;
					cnt++;
				}
	}
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		cnt = 0;
		memset(buf, 0, sizeof(buf));
		for (int i = 0; i < 2; i++) {
			int p = 0;
			cin >> s;
			draw(s, p, 0, 0, len);
		}
		cout << "There are " << cnt << " black pixels." << endl;
	}
	return 0;
}

用java语言编写程序,代码如下:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(new BufferedInputStream(System.in));
		final int len = 32;
		int T = input.nextInt();
		for(int i = 0; i < T; i++) {
			int[] cnt = new int[1];
			cnt[0] = 0;
			int[][] buf = new int[len][len];
			
			String s;
			for(int j = 0; j < 2; j++) {
				s = input.next();
				int[] p = new int[1];
				p[0] = 0;
				draw(s, p, 0, 0, len, buf, cnt);
			}
			System.out.println("There are " + cnt[0] + " black pixels.");
		}
	}
	
	public static void draw(String s, int[] p, int r, int c, int w, int[][] buf, int[] cnt) {
		char ch = s.charAt(p[0]++);
		if(ch == 'p') {
			draw(s, p, r, c + w / 2, w / 2, buf, cnt);
			draw(s, p, r, c, w / 2, buf, cnt);
			draw(s, p, r + w / 2, c, w / 2, buf, cnt);
			draw(s, p, r + w / 2, c + w / 2, w / 2, buf, cnt);
		}
		else if(ch == 'f') {
			for(int i = r; i < r + w; i++)
				for(int j = c; j < c + w; j++)
					if(buf[i][j] == 0) {
						buf[i][j] = 1;
						cnt[0]++;
					}
		}
	}
}





本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

四分树(Quadtrees) 的相关文章

  • 【Django网络安全】如何正确设置跨域

    原文作者 xff1a 我辈李想 版权声明 xff1a 文章原创 xff0c 转载时请务必加上原文超链接 作者信息和本声明 Django网络安全 Django网络安全 如何正确设置跨域 Django网络安全 如何正确防护CSRF跨站点请求伪造
  • 我怎么能只懂得使用Redis呢?(一)

    前言 最近在回顾 amp 学习Redis相关的知识 xff0c 也是本着分享的态度和知识点的记录在这里写下相关的文章 希望能帮助各位读者学习或者回顾到Redis相关的知识 xff0c 当然 xff0c 本人才疏学浅 xff0c 在学习和写作
  • 统计回文子串

    1054 统计回文子串 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 字符串处理回文串 题目描述 现在给你一个字符串S xff0c 请你计算S中有多少连续子串是回文串 输入格式 输入
  • 小明养猪的故事

    1064 小明养猪的故事 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 递推 题目描述 话说现在猪肉价格这么贵 xff0c 小明也开始了养猪生活 说来也奇怪 xff0c 他养的猪一出
  • 找新朋友

    1079 找新朋友 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 简单数学题公约数 题目描述 新年快到了 xff0c 天勤准备搞一个聚会 xff0c 已经知道现有会员N人 xff0c
  • 生物节律

    1084 生物节律 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 数学题 题目描述 有些人相信 xff0c 人从出生开始就有三个生物周期 这三个生物周期分别是体力 xff0c 情绪和
  • 鸡兔同笼

    鸡兔同笼 已知鸡和兔的总数量为n xff0c 总腿数为m 输入n和m xff0c 依次输出鸡的数目和兔的数目 如果无解 xff0c 则输出No answer 样例输入 xff1a 14 32 样例输出 xff1a 12 2 样例输入 xff
  • 赌徒

    1097 赌徒 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 查找二分查找 题目描述 有n个赌徒打算赌一局 规则是 xff1a 每人下一个赌注 xff0c 赌注为非负整数 xff0c
  • 排列(permutation)

    排列 xff08 permutation xff09 用1 2 3 xff0c xff0c 9组成3个三位数 abc xff0c def 和 ghi xff0c 每个数字恰好使用一次 xff0c 要求 abc def ghi 61 1 2
  • SQL Server Management Studio 使用方法手记

    本方法只记录自己的操作方法 xff0c 仅供参考 一 下载安装 SQL Server Management Studio V15 0 18424 0 xff0c 链接 xff1a https download csdn net downlo
  • 蛇形填数(矩阵)

    蛇形填数 在 n x n 方针里填入 1 2 xff0c xff0c n x n xff0c 要求填成蛇形 例如 xff1a n 61 4时方阵为 xff1a 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上
  • WERTYU

    Problem 1343 WERTYU Time Limit 1000 mSec Memory Limit 32768 KB Problem Description A common typing error is to place the
  • 分子量(Molar Mass)

    Molar mass Time limit 3 000 seconds An organic compound is any member of a large class of chemical compounds whose molec
  • 数数字(Digit Counting)

    Digit Counting Time limit 3 000 seconds Trung is bored with his mathematics homeworks He takes a piece of chalk and star
  • 周期串(Periodic Strings)

    周期串 Periodic Strings 如果一个字符串可以由某个长度为k的字符串重复多次得到 xff0c 则称该串以k为周期 例如 xff0c abcabcabcabc以3为周期 xff08 注意 xff0c 它也以6和12为周期 xff
  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene
  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi

随机推荐

  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce
  • Ducci序列(Ducci Sequence)

    Ducci Sequence Time limit 3 000 seconds A Ducci sequence is a sequence of n tuples of integers Given an n tuple of integ
  • 卡片游戏(Throwing cards away I)

    Problem B Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at
  • 交换学生(Foreign Exchange)

    Problem E Foreign Exchange Input standard input Output standard output Time Limit 1 second Your non profit organization
  • CAN通信物理容错测试checklist

    CAN通信物理容错测试checklist 工作项目中 xff0c 我们有时有些产品CAN总线功能 xff0c 一般情况下我们必须要满足以下几种状况的测试项才算符合要求 一 CAN H与CAN L短路 判定标准 xff1a 短接故障发生后 x
  • 对称轴(Symmetry)

    Symmetry Time limit 3 000 seconds The figure shown on the left is left right symmetric as it is possible to fold the she
  • 打印队列(Printer Queue)

    Printer Queue Time limit 3 000 seconds 分析 首先记录所求时间它在队列中的位置 xff0c 用一个队列存储这些任务的优先级 xff0c 同时也创建一个队列存储对应任务一开始的位置 xff0c 那么当我们
  • 更新字典(Updating a Dictionary)

    Updating a Dictionary Time Limit 1000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description In th
  • 铁轨(Rails)

    Rails Time limit 3 000 seconds Rails There is a famous railway station in PopPush City Country there is incredibly hilly
  • 矩阵链乘(Matrix Chain Multiplication)

    Matrix Chain Multiplication Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description
  • 天平(Not so Mobile)

    Not so Mobile Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Before being
  • 下落的树叶(The Falling Leaves)

    The Falling Leaves Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Each yea
  • 四分树(Quadtrees)

    Quadtrees Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description A quadtree is a r