uva 10562 Undraw the Trees
来源:互联网 发布:js 滚动字幕特效 编辑:程序博客网 时间:2024/06/14 19:39
将一棵多叉树转换为括号表示。直接在一个二维数组里递归。一道比较水的题,但被数据给恶心到了,‘#’不可以表示结点,注意空树的处理,(第一行为空,别的能构成一棵树,这也为空树,例如{
(此行为空)
a
|
-----
b c
}
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char a[25][205];int n;void dfs(int r, int c){ if(a[r][c] == ' ' || a[r][c] == '#') return; printf("%c(", a[r][c]); if(r+1 < n && a[r+1][c] == '|')//有子树 { int i = c; while(i-1 >= 0 && a[r+2][i-1] == '-')//找---左界 i--; while(a[r+2][i] == '-' && a[r+3][i] != '\0') { if(!isspace(a[r+3][i])) dfs(r+3, i); i++; } } printf(")");}int main(){ int t; cin >> t; getchar(); while(t--) { char s[205]; memset(a, 0, sizeof(a)); int j = 0; while(1) { gets(s); int len = strlen(s); for(int i = 0; i < len; i++) a[j][i] = s[i]; if(a[j][0] == '#') break; j++; } n = j; printf("("); for(int i = 0; i < 205; i++) { if(a[0][i] != '\0') { dfs(0, i); } } printf(")\n"); } return 0;}
阅读全文
0 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
- OSM数据下载的简易方法
- static_cast 转载from ider
- 什么是GPIO?
- 中断处理流程分析
- 关于EasyUI Datagrid JSON对象内嵌对象的取值问题
- uva 10562 Undraw the Trees
- STMFD和LDMFD指令个人理解分析
- java基础第十一天 异常
- openresty动态扩容支持持久化(完整版)
- 检测SQLServer复制订阅进度
- 串口学习
- Memcached ++++ exception thrown while trying to get object from cache for key
- DMA学习
- 像素深度BPP