UVA 297 - Quadtrees

本文介绍了一种使用递归思想来模拟绘制矩形区域的方法。通过解析输入字符串中的指令,该程序能够模拟出特定的矩形图案,并计算出黑色像素的数量。主要涉及递归调用、字符串解析等技术。

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

简单的递归思想,按照紫书的思路,“模拟”绘制相应的矩形区域即可

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
using namespace std;

int T;
int amount;
string s;
int area[32][32];

void Draw(int length,int &pos,int leftx,int lefty,int width){
	if (pos >= length) return;
	char ch = s[pos];
	pos++;
	if (ch == 'p'){
		Draw(length,pos,leftx,lefty+width/2,width/2);
		Draw(length,pos,leftx,lefty,width/2);
		Draw(length,pos,leftx+width/2,lefty,width/2);
		Draw(length,pos,leftx+width/2,lefty+width/2,width/2);
	}
	else if (ch == 'f'){
		for (int i = leftx; i < leftx + width; i++){
			for (int j = lefty; j < lefty + width; j++){
				if (area[i][j] == 0){
					area[i][j] = 1;
					amount++;
				}
			}
		}
	}
}

int main(){
	cin >> T;
	while (T--){
		amount = 0;
		memset(area,0,sizeof(area));
		for (int i = 0; i < 2; i++){
			cin >> s;
			int length = s.size();
			int pos = 0;
			int width = 32;
			Draw(length, pos, 0, 0, width);
		}
		cout << "There are " << amount<<" black pixels." << endl;
	}
	//system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值