UVa 10562 - Undraw the Trees
来源:互联网 发布:彩票源码论坛php 编辑:程序博客网 时间:2024/05/16 05:26
比较坑爹的一个题,节点不一定为字母,而且可能为空节点(需输出“()”),除去这两个地方比较恶心,个人感觉这还是比较好的一个树的递归题。
代码如下:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>char a[200+2][200+2];int cct;void dfs(int x, int y){ int i, j, k; for(i = y; a[x][i] == '-'; i ++) if(a[x+1][i] != ' ' && a[x+1][i] != '\0') { printf("%c(", a[x+1][i]); if(x + 1 < cct&& a[x + 2][i] == '|') { for(j=i; j && a[x+3][j-1] == '-'; j --); dfs(x+3, j); } printf(")"); }}int main(){#ifdef state freopen("sample.txt","r",stdin);#endif int num,i; scanf("%d",&num); getchar(); while(num--) { cct = 1; memset(a, 0, sizeof(a)); while(1) { gets(a[cct++]); if(!strcmp(a[cct-1], "#")) break; } cct--; if(cct == 1) // 树为空时,需要特殊处理 puts("()"); else { int i,len = strlen(a[1]); for(i = 0; i < len; i ++) a[0][i] = '-'; printf("("); dfs(0, 0); printf(")\n"); } } return 0;}
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- uva-10562Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA - 10562 Undraw the Trees
- UVa 10562 - Undraw the Trees
- Uva - 10562 - Undraw the Trees
- uva 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- UVa 705 - Slash Maze
- Maven无法正常下载快照Snapshots jar
- 30岁学android开发
- 一个游戏程序员的学习资料
- UVa 11111 - Generalized Matrioshkas
- UVa 10562 - Undraw the Trees
- 一些小功能的总结
- win7下安装ORACLE10g报错 未知错误
- UVa 729 - The Hamming Distance Problem
- UVa 10167 - Birthday Cake
- Spring的事务通知,2.0
- js实现页面打印
- 析构函数
- UVa 10132 - File Fragmentation