习题6-8 空间结构(Spatial Structures, ACM/ICPC World Finals 1998, UVa806)

输出格式上需要注意细节

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

int n;
char buf[100][100];
int s[100];
int v[10000];
vector<int> ans;

//十进制数转成翻转的五进制 
void ten_to_five(int x) {
	int cnt = 0;
	memset(s, 0, sizeof(s));
	while(x > 0) {
		int b = x % 5;
		s[cnt++] = b;
		x = x / 5;
	}
	s[cnt] = -1;
}

//将五进制数翻转后,转成十进制 
int five_to_ten(int x) {
	int y = 0;
	while(x > 0) {
		y = y*10 + x%10;
		x = x/10;
	}
	int cnt = 0, res = 0;
	while(y > 0) {
		int b = y%10;
		for(int i = 0; i < cnt; i++) b *= 5;
		res += b; 
		y = y/10;
		cnt++;
	}
	return res;
}

//把字符串s[p..]导出到以(r,c)为左上角,边长为w的缓冲区中
//1 2
//3 4
void draw(const int* s, int& p, int r, int c, int w) {
	int num = s[p++];
	switch(num) {
		case -1:
			for(int i = r; i < r + w; i++) {
				for(int j = c; j < c + w; j++) {
					buf[i][j] = '1';
				}
			}
			break;
		case 1:
			draw(s, p, r, c, w/2);
			break;
		case 2:
			draw(s, p, r, c + w/2, w/2);
			break;
		case 3:
			draw(s, p, r + w/2, c, w/2);
			break;
		case 4:
			draw(s, p, r + w/2, c + w/2, w/2);
			break;
	}
}

int check(int r, int c, int w) {
	for(int i = r; i < r + w; i++) {
		for(int j = c; j < c + w; j++) {
			if(buf[i][j] != '1') return 0;
		}
	}
	return 1;
}

void solve(int r, int c, int w, int num) {
	if(check(r, c, w)) {
		ans.push_back(0);return;
	}
	if(w == 1) return;
	if(check(r, c, w/2)) {
		ans.push_back(five_to_ten(num * 10 + 1));
	} else {
		solve(r, c, w/2, num*10 + 1);
	}
	
	if(check(r, c + w/2, w/2)) {
		ans.push_back(five_to_ten(num * 10 + 2));
	} else {
		solve(r, c + w/2, w/2, num*10 + 2);
	}
	
	if(check(r + w/2, c, w/2)) {
		ans.push_back(five_to_ten(num * 10 + 3));
	} else {
		solve(r + w/2, c, w/2, num*10 + 3);
	}
	
	if(check(r + w/2, c + w/2, w/2)) {
		ans.push_back(five_to_ten(num * 10 + 4));
	} else {
		solve(r + w/2, c + w/2, w/2, num*10 + 4);
	}
}

int main() {
	#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	int q = 0;
	while(scanf("%d", &n) == 1 && n) {
		if(q > 0) printf("\n");
		memset(buf, '0', sizeof(buf));
		memset(v, 0, sizeof(v));
		printf("Image %d\n", ++q);
		if(n > 0) {
			for(int i = 0; i < n; i++) {
				scanf("%s", buf[i]);
			}
			ans.clear();
			solve(0, 0, n, 0);
			sort(ans.begin(), ans.end());
			int cnt = 0, len = ans.size();
			for(int i = 0; i < len; i++) {
				if(cnt > 0) printf(" ");
				printf("%d", ans[i]);
				if(++cnt == 12) {
					printf("\n");
					cnt = 0;
				}
			}
			if(cnt != 0 && len > 0) printf("\n");
			printf("Total number of black nodes = %d\n", ans.size());
		} else {
			n = -1 * n;
			int cnt = 0, x;
			while(scanf("%d", &x) == 1 && x != -1) {
				v[cnt++] = x;
			}
			for(int i = 0; i < cnt; i++) {
				ten_to_five(v[i]);
				int p = 0;
				draw(s, p, 0, 0, n);
			}
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < n; j++) {
					if(buf[i][j] == '1') printf("*");
					else printf(".");
				}
				printf("\n");
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值