UVA 10562 Undraw the Trees

#include <cstdio>
#include <cstring>
char tree[210][210];

void write(int line, int star) {
	for (int j = star; line ? tree[line - 1][j] == '-' : j < 209; j++) //如果是 0 行就数到行末, 否者数到不等于‘-’
		if ( tree[line][j] != ' ' && tree[line][j] != 0 ) { //判断是不是结点
			printf("%c(", tree[line][j]);
			if (tree[line + 1][j] == '|') {    //判断有没有子结点
				int	left; 
				for (left = j ; left && tree[line + 2][left - 1] ==  '-' ; left--); //寻找第一个子结点的起始位置
				write(line + 3, left);
			}
			printf(")");
		}
}

int main() {
	int t, n;
	scanf("%d", &t);
	getchar();
	while (t--) {
		memset(tree, 0, sizeof(tree));
		for (n = 0; gets(tree[n]) && tree[n][0] != '#'; n++); //计算树有多少行
		printf("(");
		if (n)  //判断是否是空树
			write(0, 0);
		printf(")\n");
	}
	return 0;
}

你可能感兴趣的:(UVA 10562 Undraw the Trees)