297 - Quadtrees

简单点实现可以不用把各个节点连接起来建树,递归的扫描一遍,根据每个节点的表示的跨度范围和类型填充一个1024的数组,每个case中两个树依次填充完毕,最后统计数组中1的个数即为结果。

 

 

#include<stdio.h>

char str[2000];
int num[1024];
int idx;
void buildTree(char *s, int start, int end) {

	char type = s[idx++];
	if (type == 'p') {
		int mid = (start + end) / 2;
		int lmid = (start + mid) / 2;
		int rmid = (mid + end) / 2;
		buildTree(s, start, lmid);
		buildTree(s, lmid + 1, mid);
		buildTree(s, mid + 1, rmid);
		buildTree(s, rmid + 1, end);
	} else if (type == 'f') {
		int i;
		for (i = start; i <= end; i++)
			num[i] = 1;
	}
}
int main() {
	int cases;
	scanf("%d", &cases);
	while (cases--) {
		idx = 0;
		int i;
		for (i = 0; i < 1024; i++)
			num[i] = 0;

		scanf("%s", str);
		buildTree(str, 0, 1023);

		idx = 0;
		scanf("%s", str);
		buildTree(str, 0, 1023);

		int sum = 0;
		for (i = 0; i < 1024; i++) {
			sum += num[i];
		}
		printf("There are %d black pixels.\n", sum);

	}
	return 0;
}

 

你可能感兴趣的:(dtree)