UVa10562 Undraw the Trees
注意点:
空树情况处理。
循环时候可以先判断符合条件,再递减:
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的更多相关文章
- [DFS遍历图]UVA10562 Undraw the Trees
传送门: 1. UVA - 10562 2. Vjudge [看图写树] 将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...
- Uva10562——Undraw the Trees
上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
- UVa 10562 Undraw the Trees 看图写树
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...
- uva 10562 undraw the trees(烂题) ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...
- UVa 10562 (特殊的输入处理方式) Undraw the Trees
题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...
- UVa 10562 Undraw the Trees
题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...
随机推荐
- poj 1236 Network of Schools
题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...
- MySQL压测中遇到的一些问题
批量insert http://blog.csdn.net/xiaoxian8023/article/details/20155429 Mysql jdbc 批处理数据,需要给jdbc连接加上rewr ...
- Webdriver API (三)- actions
Actions类主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用perform()方法进行执行. actions类可以完成单一的操作,也可以完成几个操作的组合. 有 ...
- 关于jQuery中,animate、slide、fade等动画的连续触发、滞后反复执行的bug的个人解决办法
照例,现在开头讲个这个问题发生的背景吧: 因为最近要做个操作选项的呼出,然后就想到了用默认隐藏,鼠标划过的时候显示的方法. 刚开始打算添加一个class="active",直接触发 ...
- Web服务器(Apache)虚拟主机的配置
一.定义 所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务. 二.虚拟主机的实现方法有三种: 基于IP的方法,基于主机名的方法和基于端口的法官法. ...
- C语言-简单哈希表(hash table)
腾讯三面的时候,叫我写了个哈希表,当时紧张没写好···结果跪了··· 回来后粪发涂墙,赶紧写了一个! 什么都不说了···先让我到厕所里面哭一会··· %>_<% 果然现场发挥,以及基础扎实 ...
- CSS快速制作图片轮播的焦点
来源:http://www.ido321.com/858.html 效果图: 演示地址:http://jsfiddle.net/Web_Code/q5qfd8aL/embedded/result/ 代 ...
- MYSQL里的索引类型介绍
首先要明白索引(index)是在存储引擎(storage engine)层面实现的,而不是在server层面.不是所有的存储引擎支持有的索引类型. 1.B-TREE 最常见的索引类型,他的思想是所有的 ...
- 【恒天云技术分享系列10】OpenStack块存储技术
原文:http://www.hengtianyun.com/download-show-id-101.html 块存储,简单来说就是提供了块设备存储的接口.用户需要把块存储卷附加到虚拟机(或者裸机)上 ...
- NodeJS学习:爬虫小探
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 今天来学习alsotang的爬虫教程,跟着把CNode简单地爬一遍. 建立项目craelr-demo 我们首先建立一个Expr ...