注意点:

空树情况处理。

循环时候可以先判断符合条件,再递减:

while(i-1>=0 && buf[r+2][i-1]=='-') i--;

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<string>
  5. #include<algorithm>
  6. using namespace std;
  7. const int N=200+2;
  8. char buf[N][N];
  9. int n;
  10.  
  11. //递归遍历并且输出以字符buf[r][c]为根的树
  12. void dfs(int r, int c)
  13. {
  14. printf("%c(", buf[r][c]);
  15. //看看是否有子节点
  16. if(r+1<n && buf[r+1][c]=='|')
  17. {
  18. //找最左边界i
  19. int i=c;
  20. while(i-1>=0 && buf[r+2][i-1]=='-') i--;
  21. while(buf[r+2][i]=='-' && buf[r+3][i]!=0)
  22. {
  23. if(!isspace(buf[r+3][i]))
  24. dfs(r+3, i);
  25. i++;
  26. }
  27. }
  28. printf(")");
  29. }
  30.  
  31. int main()
  32. {
  33. #ifndef ONLINE_JUDGE
  34. freopen("./uva10562.in", "r", stdin);
  35. #endif
  36. int T;
  37. gets(buf[0]);
  38. sscanf(buf[0], "%d", &T);
  39. while(T--)
  40. {
  41. n=0;
  42. while(1)
  43. {
  44. gets(buf[n]);
  45. if(buf[n][0]=='#')
  46. break;
  47. n++;
  48. }
  49.  
  50. printf("(");
  51. //注意处理空树
  52. if(n)
  53. {
  54. for(int i=0;i<strlen(buf[0]);i++)
  55. {
  56. if(buf[0][i]!=' ')
  57. {
  58. dfs(0, i);
  59. }
  60. }
  61. }
  62. printf(")\n");
  63. }
  64.  
  65. return 0;
  66. }

 

  1. // UVa10562 Undraw the Trees
  2. // Rujia Liu
  3. // 题意:把画得挺好看的多叉树转化为括号表示法
  4. // 算法:直接在二维字符数组里递归。注意空树,并且结点标号可以是任意可打印字符
  5.  
  6. #include<cstdio>
  7. #include<cctype>
  8. #include<cstring>
  9. using namespace std;
  10.  
  11. const int maxn = 200 + 10;
  12. int n;
  13. char buf[maxn][maxn];
  14.  
  15. // 递归遍历并且输出以字符buf[r][c]为根的树
  16. void dfs(int r, int c) {
  17. printf("%c(", buf[r][c]);
  18. if(r+1 < n && buf[r+1][c] == '|') { // 有子树
  19. int i = c;
  20. while(i-1 >= 0 && buf[r+2][i-1] == '-') i--; // 找"----"的左边界
  21. while(buf[r+2][i] == '-' && buf[r+3][i] != '\0') {
  22. if(!isspace(buf[r+3][i])) dfs(r+3, i); // fgets读入的'\n'也满足isspace()
  23. i++;
  24. }
  25. }
  26. printf(")");
  27. }
  28.  
  29. void solve() {
  30. n = 0;
  31. for(;;) {
  32. fgets(buf[n], maxn, stdin);
  33. if(buf[n][0] == '#') break; else n++;
  34. }
  35. printf("(");
  36. if(n) {
  37. for(int i = 0; i < strlen(buf[0]); i++)
  38. if(buf[0][i] != ' ') { dfs(0, i); break; }
  39. }
  40. printf(")\n");
  41. }
  42.  
  43. int main() {
  44. int T;
  45. fgets(buf[0], maxn, stdin);
  46. sscanf(buf[0], "%d", &T);
  47. while(T--) solve();
  48. return 0;
  49. }

UVa10562 Undraw the Trees的更多相关文章

  1. [DFS遍历图]UVA10562 Undraw the Trees

    传送门: 1. UVA - 10562 2. Vjudge [看图写树]     将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...

  2. Uva10562——Undraw the Trees

    上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...

  3. UVa 10562 Undraw the Trees(递归遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...

  4. 看图写树 (Undraw the Trees UVA - 10562)

    题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...

  5. UVa 10562 Undraw the Trees 看图写树

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...

  6. uva 10562 undraw the trees(烂题) ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  7. UVa 10562 (特殊的输入处理方式) Undraw the Trees

    题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...

  8. UVa 10562 Undraw the Trees

    题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...

  9. 【紫书】Undraw the Trees UVA - 10562 递归,字符串

    题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...

随机推荐

  1. poj 1236 Network of Schools

    题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...

  2. MySQL压测中遇到的一些问题

    批量insert http://blog.csdn.net/xiaoxian8023/article/details/20155429 Mysql jdbc 批处理数据,需要给jdbc连接加上rewr ...

  3. Webdriver API (三)- actions

    Actions类主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用perform()方法进行执行. actions类可以完成单一的操作,也可以完成几个操作的组合. 有 ...

  4. 关于jQuery中,animate、slide、fade等动画的连续触发、滞后反复执行的bug的个人解决办法

    照例,现在开头讲个这个问题发生的背景吧: 因为最近要做个操作选项的呼出,然后就想到了用默认隐藏,鼠标划过的时候显示的方法. 刚开始打算添加一个class="active",直接触发 ...

  5. Web服务器(Apache)虚拟主机的配置

    一.定义    所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务.    二.虚拟主机的实现方法有三种:    基于IP的方法,基于主机名的方法和基于端口的法官法.    ...

  6. C语言-简单哈希表(hash table)

    腾讯三面的时候,叫我写了个哈希表,当时紧张没写好···结果跪了··· 回来后粪发涂墙,赶紧写了一个! 什么都不说了···先让我到厕所里面哭一会··· %>_<% 果然现场发挥,以及基础扎实 ...

  7. CSS快速制作图片轮播的焦点

    来源:http://www.ido321.com/858.html 效果图: 演示地址:http://jsfiddle.net/Web_Code/q5qfd8aL/embedded/result/ 代 ...

  8. MYSQL里的索引类型介绍

    首先要明白索引(index)是在存储引擎(storage engine)层面实现的,而不是在server层面.不是所有的存储引擎支持有的索引类型. 1.B-TREE 最常见的索引类型,他的思想是所有的 ...

  9. 【恒天云技术分享系列10】OpenStack块存储技术

    原文:http://www.hengtianyun.com/download-show-id-101.html 块存储,简单来说就是提供了块设备存储的接口.用户需要把块存储卷附加到虚拟机(或者裸机)上 ...

  10. NodeJS学习:爬虫小探

    说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 今天来学习alsotang的爬虫教程,跟着把CNode简单地爬一遍. 建立项目craelr-demo 我们首先建立一个Expr ...