博客
关于我
例题6-17 看图写树(Undraw the Trees, UVa 10562)
阅读量:292 次
发布时间:2019-03-03

本文共 921 字,大约阅读时间需要 3 分钟。

原题链接:

分类:树
备注:多叉树的DFS

因为最外面那个括号让我搞了半天…看一眼紫书,原来最外层的括号和里面的括号规律不同,直接放在外面输出就好了…

代码如下:

#include
#include
#include
using namespace std;string line[205];int depth;void dfs(int L,int R,int pos) { for (int i = L; i < R && i < line[pos].length(); i++) { if (line[pos][i] != ' ' && line[pos][i] != '-' && line[pos][i] != '#') { printf("%c(", line[pos][i]); if (pos + 1 < depth && i < line[pos + 1].length() && line[pos + 1][i] == '|') { int l, r; l = r = i; while (l > 0 && line[pos + 2][l - 1] != ' ')l--; while (line[pos + 2][r] != ' ' && r < line[pos + 2].length())r++; dfs(l, r, pos + 3); } printf(")"); } }}int main(void) { int T; scanf("%d", &T); getchar(); while (T--) { depth = 0; while (getline(cin, line[depth++])) { if (line[depth - 1] == "#")break; } depth--; printf("("); if (depth)dfs(0, line[0].length(), 0); printf(")\n"); } return 0;}

转载地址:http://gcel.baihongyu.com/

你可能感兴趣的文章
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>