题目描述:

原题:https://vjudge.net/problem/UVA-10562

题目思路:

递归找结点

//自己的代码测试过了,一直WA,贴上紫书的代码

AC代码

#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std; const int maxn = + ;
int n;
char buf[maxn][maxn]; // 递归遍历并且输出以字符buf[r][c]为根的树
void dfs(int r, int c) {
printf("%c(", buf[r][c]);
if(r+ < n && buf[r+][c] == '|') { // 有子树
int i = c;
while(i- >= && buf[r+][i-] == '-') i--; // 找"----"的左边界
while(buf[r+][i] == '-' && buf[r+][i] != '\0') {
if(!isspace(buf[r+][i])) dfs(r+, i); // fgets读入的'\n'也满足isspace()
i++;
}
}
printf(")");
} void solve() {
n = ;
for(;;) {
fgets(buf[n], maxn, stdin);
if(buf[n][] == '#') break; else n++;
}
printf("(");
if(n) {
for(int i = ; i < strlen(buf[]); i++)
if(buf[][i] != ' ') { dfs(, i); break; }
}
printf(")\n");
} int main() {
int T;
fgets(buf[], maxn, stdin);
sscanf(buf[], "%d", &T);
while(T--) solve();
return ;
}

看图写树 (Undraw the Trees UVA - 10562)的更多相关文章

  1. UVa 10562 Undraw the Trees 看图写树

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

  2. UVa 10562看图写树(二叉树遍历)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. Uva 10562 看图写树

    题目链接:https://uva.onlinejudge.org/external/105/10562.pdf 紫书P170 直接在二维数组上做DFS,用的fgets函数读入数据,比较gets函数安全 ...

  4. UVA10562(看图写树,dfs)

    这个题过的好艰难,不过真的学到好多. 关于fgets的用法真的是精髓.!isspace(c)和c!=' '是有区别的. 其它的看代码吧 #include <iostream> #inclu ...

  5. 6-17 看图写树 uva10562

    非常好的dfs题  有很多细节 关于‘ ’  ‘0’  ’\n‘  的处理  他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...

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

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

  7. 看图写代码---看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >>

    看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >> 1.S ...

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

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

  9. 看图轻松理解数据结构与算法系列(NoSQL存储-LSM树) - 全文

    <看图轻松理解数据结构和算法>,主要使用图片来描述常见的数据结构和算法,轻松阅读并理解掌握.本系列包括各种堆.各种队列.各种列表.各种树.各种图.各种排序等等几十篇的样子. 关于LSM树 ...

随机推荐

  1. Gradle Goodness: Running Java Applications from External Dependency

    With Gradle we can execute Java applications using the JavaExec task or the javaexec() method. If we ...

  2. java向邮箱发送消息失败!

    出现的错误如下: org.apache.commons.mail.EmailException: Sending the email to the following server failed : ...

  3. 二、HDFS 架构

    源自:http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html HDFS has a m ...

  4. python drf+xadmin+react+dva+react-native+sentry+nginx 搭建前后端分离的博客完整平台

    前言: 经过差不多半年的开发,搭建从前端到服务器,实现了前后端分离的一个集PC端.移动端的多端应用,实属不易,今天得空,好好写篇文章,记录这些天的成果.同时也做个分享. 演示网站地址: http:// ...

  5. Ldap实现AD域认证

    1.java Ldap基础类 package com.common; import java.io.FileInputStream; import java.io.IOException; impor ...

  6. JSP/Servlet开发——第七章 Servel基础

    1.Servlet简介: ●Servlet是一个符合特定规范的 JAVA 程序 , 是一个基于JAVA技术的Web组件. ●Servlet允许在服务器端,由Servlet容器所管理,用于处理客户端请求 ...

  7. canvas 将html绘制图片 生成图片链接

    废话不多说,直接上代码 其中图片地址换成你的,自己玩儿去吧 <!DOCTYPE html> <html> <head> <meta charset=" ...

  8. vowels_单元音

    vowels(美式): 单元音: [i]:需要用劲喊出类似于“yi”的四声,费力咧开嘴,单词eat.need.thief.meet. [?]:卷舌音,单词bird.her.worry.certain. ...

  9. appium+python解决每次运行代码都提示安装Unlock以及AppiumSetting的问题

    appium+python解决每次运行代码都提示安装Unlock以及AppiumSetting的问题(部分安卓机型) 1.修改appium-android-driver\lib下的android-he ...

  10. HDU3394 点双连通分量

    Railway Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...