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

Posted As_zyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了习题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;
}

以上是关于习题6-8 空间结构(Spatial Structures, ACM/ICPC World Finals 1998, UVa806)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

C和指针 第十章 结构和联合 习题

习题3.4 最长连续递增子序列(20 分)浙大版《数据结构(第2版)》题目集

[PTA]习题6-8 统计一行文本的单词个数

习题6-8 统计一行文本的单词个数(15 分)