菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

题意:

 

一个四叉树用来格式编码图像,这个想法的基础是任意的图像能够分成四个象限.每个象限能够拆分成四个子象限,
比如,在四叉树中,每一个图像都被一个父节点表示,根据预先定义好的顺序,四个孩子节点代表四个象限.

当然,如果整个图像只有一个颜色,这个图像的四叉树形式只有一个节点.通常,如果一个象限由不同的颜色组成,这个象限需要再次被拆分,
因此,四叉树不必有均匀的深度.
现代电脑图像工作基础是32x32单元格的黑白图,所以一个图像有1024个像素点.为了形成一个新的图像,一个操作是把俩个图像加在一起。
如果俩个像素点中有一个是黑色,那么相加后的像素点是黑色,要不然就是白色.

This particular artist believes in what he calls the preferred fullness:
为了让一个图像变得有趣(比如卖更多的钱)的最重要的属性是被填满黑色像素点的数目.
所以在让俩个图像相加前,他想知道在相加后的图像中有多少个黑色的像素点.
你的任务是写一个程序,给一颗代表俩个图形的四叉树,计算相加后结果图像中黑色的像素点的数目,

比如下面的计算,第一个样例从上到下展示了一个图像,四叉树,先序遍历字符串形式,像素点数目,
象限编号在最上方显示。
————
| 2|1 |
| 3|4 |
————
图形
四叉树形式
字符串形式
四个象限总共1024个点,比如第一象限占的点数是1024/4=256,样例中一个最小黑方块占的点数是256/4=64

输入:
第一行包含测试用例总数(N)
每一组测试用例包含俩个字符串,每个字符串一行,字符串是四叉树的先序遍历结果
字符p代表父节点,f代表象限全是黑色,e代表全是白色,保证每个字符串的是合法的四叉树,
树的深度不会超过5(因为这样像素的只有一个颜色)
1024
/
256
/
64
/
16
/
4
/
1

 

 

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<memory.h>
using namespace std;

const int MAX = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6) + (1 << 8)
		+ (1 << 10);
void buildTree(char* a, string str, int index, int* sIndex);
void addTree(const char* t1, const char* t2, int* total, int depth, int index);
int main()
{
	freopen("d:\\1.txt", "r", stdin);
	int N;
	cin >> N;
	string str = "There are %d black pixels.\n";
	while (N--)
	{
		char t1[MAX];
		char t2[MAX];
		memset(t1, 0, MAX);
		memset(t2, 0, MAX);
		string str1, str2;
		cin >> str1;
		cin >> str2;
		char c = str1.at(0);
		t1[0] = c;
		if(c == 'p')
		{
			int i = 0;
			buildTree(t1, str1, 0, &i);
		}
		c = str2.at(0);
		t2[0] = c;
		if(c == 'p')
		{
			int i = 0;
			buildTree(t2, str2, 0, &i);
		}
		int total = 0;
		addTree(t1, t2, &total, 0, 0);
		printf(str.c_str(), total);
	}
}

void buildTree(char* a, string str, int index, int* sIndex)
{
	char c;
	for(int i = 1; i <= 4; i++)
	{
		*sIndex = *sIndex + 1;
		c = str.at(*sIndex);
		a[index * 4 + i] = c;
		if(c == 'p')
		{
			buildTree(a, str, index * 4 + i, sIndex);
		}
	}
}

void addTree(const char* t1, const char* t2, int* total, int depth, int index)
{
	if(t1[index] == 'f' || t2[index] == 'f')
	{
		*total = *total + (1 << ((5-depth)*2));
		return;
	}
	else if(t1[index] == 'p' || t2[index] == 'p')
	{
		addTree(t1, t2, total, depth + 1, index * 4 + 1);
		addTree(t1, t2, total, depth + 1, index * 4 + 2);
		addTree(t1, t2, total, depth + 1, index * 4 + 3);
		addTree(t1, t2, total, depth + 1, index * 4 + 4);
	}

}

  

  

posted on 2017-05-07 14:52  好吧,就是菜菜  阅读(210)  评论(0编辑  收藏  举报