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

原题链接:https://vjudge.net/problem/UVA-806
分类:四分树
备注:DFS,水题

代码如下:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 64;
int n, g[maxn][maxn], id, kase;
char Graph[maxn][maxn];
vector<int>path, ans;
bool check(int x,int y,int size) {
	for (int i = x; i < x + size; i++)
		for (int j = y; j < y + size; j++)
			if (g[i][j] != g[x][y])return false;
	return true;
}
int reserve(int x) {
	int ret = 0;
	while (x) {
		ret = ret * 10 + x % 10;
		x /= 10;
	}
	return ret;
}
void solve(int x, int y, int size, int bef) {
	if (check(x, y, size / 2)) {
		if (g[x][y]) ans.push_back(reserve(bef * 10 + 1));
	}
	else solve(x, y, size / 2, bef * 10 + 1);
	if (check(x, y + size / 2, size / 2)) {
		if (g[x][y + size / 2]) ans.push_back(reserve(bef * 10 + 2));
	}
	else solve(x, y + size / 2, size / 2, bef * 10 + 2);
	if (check(x + size / 2, y, size / 2)) {
		if (g[x + size / 2][y]) ans.push_back(reserve(bef * 10 + 3));
	}
	else solve(x + size / 2, y, size / 2, bef * 10 + 3);
	if (check(x + size / 2, y + size / 2, size / 2)) {
		if (g[x + size / 2][y + size / 2]) ans.push_back(reserve(bef * 10 + 4));
	}
	else solve(x + size / 2, y + size / 2, size / 2, bef * 10 + 4);
}
void solve2() {
	for (int i = 0; i < path.size(); i++) {
		int size = n, x = 0, y = 0;
		while (path[i]) {
			int dir = path[i] % 5;
			path[i] /= 5;
			size /= 2;
			if (dir == 2) y += size;
			else if (dir == 3) x += size;
			else if (dir == 4) x += size, y += size;
		}
		for (int i = x; i < x + size; i++)
			for (int j = y; j < y + size; j++)
				Graph[i][j] = '*';
	}
}
int read() {
	char ch = getchar();
	while (ch == '\n' || ch == ' ')ch = getchar();
	return ch - '0';
}
int main(void) {
	while (~scanf("%d", &n) && n) {
		if (kase)printf("\n");
		printf("Image %d\n", ++kase);
		if (n > 0) {
			ans.clear();
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					g[i][j] = read();
			if (!check(0, 0, n))solve(0, 0, n, 0);
			else if (g[0][0])ans.push_back(0);
			sort(ans.begin(), ans.end());
			for (int i = 0, num = 0; i < ans.size(); i++) {
				int out = 0, tmp = 1;
				while (ans[i]) {
					out += ans[i] % 10 * tmp;
					tmp *= 5; ans[i] /= 10;
				}
				if (num)printf(" ");
				printf("%d", out);
				num++;
				if (num == 12) {
					num = 0;
					printf("\n");
				}
			}
			if (ans.size() % 12)printf("\n");
			printf("Total number of black nodes = %d\n", ans.size());
		}
		else {
			n = -n;
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++)
					Graph[i][j] = '.';
			path.clear();
			while (1) {
				int x; scanf("%d", &x);
				if (x == -1)break;
				path.push_back(x);
			}
			solve2();
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++)
					printf("%c", Graph[i][j]);
				printf("\n");
			}
		}
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值