注意点:

空树情况处理。

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

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. I.MX6 Power off register hacking

    /*********************************************************************** * I.MX6 Power off register ...

  2. 学习PHP C扩展之面向对象开发方式 (转)

    PHP OOP面向对象之C语言开发方式 学习PHP C扩展有一段时间了,PHP手册里大部分讲的PHP的函数开发方式,网上找OOP资料比较少,想起上个月测试redis 的时候,下载PHP扩展redis源 ...

  3. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  4. Liunx 配置IDE

    如果你还没装编译环境或自己不确定装没装,不妨先执行 sudo apt-get install build-essential 如果你已经了解一些 vim 的知识,而且想用它来编辑源代码,那么我们不妨装 ...

  5. Java应用调优指南之-工具篇

    1. 土法调优两大件 先忆苦思甜,一般人在没有Profile工具的时候,调优的两大件,无非Heap Dump 与 Thread Dump. 1.1 Heap Dump jmap -dump:live, ...

  6. android 布局居中

    android:layout_alignParentLeft="true" 位于父容器左上角 android:layout_alignParentBottom, android:l ...

  7. NSThread 多线程相关

    1.下面的代码,有2点需要注意,1>就是 就是thread:所传得参数,这里传得的是nsarray 当然也可以传其他的类型.2> [self performSelectorOnMainTh ...

  8. C++ 使用Htmlcxx解析Html内容(VS编译库文件)

    1.下载Htmlcxx,http://sourceforge.net/projects/htmlcxx/ 2.解压htmlcxx-0.85.tar.gz 3.打开htmlcxx.vcproj,注意是h ...

  9. PHP 正则表达式总结

    可以用字符作为一个通配符来代替除换行符(\n)之外的任一个字符.例如,正则表达式:.at可以与"cat"."sat"."#at"和" ...

  10. 常用SQL语句汇总整理

    1.SQL 插入语句得到自动生成的递增ID 值 insert into Table1(Name,des,num) values (''ltp'',''thisisbest'',10); select ...