数据结构 递归 uva 10562 - Undraw the Trees
来源:互联网 发布:软件侵权判刑吗 编辑:程序博客网 时间:2024/04/30 15:22
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1503
题目大意:
将给定形式的一颗树输出成另外形式的一棵树。
解题思路:
用递归模拟建树过程,边建树边输出。
代码:
#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<stack>#include<queue>#define eps 1e-6#define INF (1<<20)#define PI acos(-1.0)using namespace std;int sumrow;char save[220][220]; //边建树边输出结果bool notin(char a) //处理字符情况{ if(a!='-'&&a!=' '&&a!='#'&&a!='|'&&a) return true; return false;}void dfs(int row,int column){ putchar(save[row][column]); if(save[row+1][column]!='|') //叶子节点 { printf("()"); return ; } //非叶子节点 int left,right; putchar('('); left=right=column; while(save[row+2][--left]=='-') //向左扫描'-'覆盖范围 ; left++; while(save[row+2][++right]=='-') //向右扫描'-'覆盖范围 ; right--; for(int i=left;i<=right&&i<strlen(save[row+3]);i++) if(notin(save[row+3][i])) { dfs(row+3,i); } putchar(')'); return ;}int main(){ int ca; scanf("%d",&ca); getchar(); while(ca--) { sumrow=0; memset(save,0,sizeof(save)); while(gets(save[++sumrow])&&save[sumrow][0]!='#') ; sumrow--; if(sumrow==0) //没有考虑空树的情况,坑啊,wa了好几次 { printf("()\n"); continue; } for(int i=0;i<strlen(save[1]);i++) //扫描树根的情况。 if(notin(save[1][i])) { putchar('('); dfs(1,i); putchar(')'); } putchar('\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 - 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
- 常见协议在TCP/IP层中的分布
- 北京千禧大酒店。
- onenote2007屏幕剪辑或插入图片是报错。
- UNIX系统编程(1)
- ssh 配置 及详细信息
- 数据结构 递归 uva 10562 - Undraw the Trees
- hibernate 根据实体类自动生成表
- 日志第一页:XCode4与Cocos2d-x,所谓的开发环境搭建。
- UNIX系统编程(2)
- WindowsAPI笔记(一)
- 一个游戏程序员的学习资料
- HRBEU 石头剪刀布1
- PHP URL 重定向 的三种方法
- 背包问题九讲笔记_01背包