注意点:

空树情况处理。

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

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N=200+2;
char buf[N][N];
int n; //递归遍历并且输出以字符buf[r][c]为根的树
void dfs(int r, int c)
{
printf("%c(", buf[r][c]);
//看看是否有子节点
if(r+1<n && buf[r+1][c]=='|')
{
//找最左边界i
int i=c;
while(i-1>=0 && buf[r+2][i-1]=='-') i--;
while(buf[r+2][i]=='-' && buf[r+3][i]!=0)
{
if(!isspace(buf[r+3][i]))
dfs(r+3, i);
i++;
}
}
printf(")");
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva10562.in", "r", stdin);
#endif
int T;
gets(buf[0]);
sscanf(buf[0], "%d", &T);
while(T--)
{
n=0;
while(1)
{
gets(buf[n]);
if(buf[n][0]=='#')
break;
n++;
} printf("(");
//注意处理空树
if(n)
{
for(int i=0;i<strlen(buf[0]);i++)
{
if(buf[0][i]!=' ')
{
dfs(0, i);
}
}
}
printf(")\n");
} return 0;
}

 

// UVa10562 Undraw the Trees
// Rujia Liu
// 题意:把画得挺好看的多叉树转化为括号表示法
// 算法:直接在二维字符数组里递归。注意空树,并且结点标号可以是任意可打印字符 #include<cstdio>
#include<cctype>
#include<cstring>
using namespace std; const int maxn = 200 + 10;
int n;
char buf[maxn][maxn]; // 递归遍历并且输出以字符buf[r][c]为根的树
void dfs(int r, int c) {
printf("%c(", buf[r][c]);
if(r+1 < n && buf[r+1][c] == '|') { // 有子树
int i = c;
while(i-1 >= 0 && buf[r+2][i-1] == '-') i--; // 找"----"的左边界
while(buf[r+2][i] == '-' && buf[r+3][i] != '\0') {
if(!isspace(buf[r+3][i])) dfs(r+3, i); // fgets读入的'\n'也满足isspace()
i++;
}
}
printf(")");
} void solve() {
n = 0;
for(;;) {
fgets(buf[n], maxn, stdin);
if(buf[n][0] == '#') break; else n++;
}
printf("(");
if(n) {
for(int i = 0; i < strlen(buf[0]); i++)
if(buf[0][i] != ' ') { dfs(0, i); break; }
}
printf(")\n");
} int main() {
int T;
fgets(buf[0], maxn, stdin);
sscanf(buf[0], "%d", &T);
while(T--) solve();
return 0;
}

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. 【WEB】jsp向servlet传参中文乱码问题解决

    传参方式:POST.GET.link方式 servlet向jsp传中文参数msg if(username.equals("") || password.euqals("& ...

  2. Oracle 课程八之性能优化之10053事件

    一. 10053事件 当一个SQL出现性能问题的时候,可以使用SQL_TRACE 或者 10046事件来跟踪SQL. 通过生成的trace来了解SQL的执行过程. 我们在查看一条SQL的执行计划的时候 ...

  3. mysql教程-触发器

    触发器 1. mysql触发器 情景说明 情景设置,如图,当我们点击了购买,将会发生什么? 现有如下两张表 商品表 编号(id)名称(name)价格(price)库存(stock) 1F2战斗机100 ...

  4. mysql5.6子查询的优化

    https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html Semi-join in MySQL 5.6   MySQL 5. ...

  5. [译]LINT TO SQL 介绍(数据库查询) - Part.3

    出处:Linq To Sql (Part.3 – Querying our database) 术语表 Built-in:内置的 Clause:子句 Debugger:调试器 Object Relat ...

  6. 有效处理java异常的三个原则

    Java中异常提供了一种识别及响应错误情况的一致性机制,有效地异常处理能使程序更加健壮.易于调试.异常之所以是一种强大的调试手段,在于其回答了以下三个问题: 什么出了错? 在哪出的错? 为什么出错? ...

  7. 火狐和google游览器的 hack独有识别 css

    先来看google的: /* 这针对于webkit内核的游览器.包括苹果谷歌游览器等*/ @media screen and (-webkit-min-device-pixel-ratio:0) { ...

  8. android电池信息简介

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  9. CSS 3动画介绍

    原文:A Beginner’s Introduction to CSS Animation 译文:一个初学者对CSS动画的介绍 译者:dwqs 现在,越来越多的网站使用了动画,并且形式多样,如GIF. ...

  10. bzoj 2959 长跑(LCT+BCC+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2959 [题意] n个点,提供操作:连边,修改点权,查询自定义边的方向后起点a终点b能经 ...