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 ...
随机推荐
- TS数据结构分析
1.TS包得数据结构 2. // Transport packet headertypedef struct TS_packet_header{ unsigned sync_byte ...
- mysql show processlist命令 详解
SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...
- SQL Server 高性能写入的一些总结(转)
1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...
- Linux+Apache+Tomcat集群配置
参考: http://blog.csdn.net/bluishglc/article/details/6867358# http://andashu.blog.51cto.com/8673810/13 ...
- 【Windows核心编程】VirtualAlloc 例子
// VirtualAlloc.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #in ...
- 纯CSS制作冒泡提示框
来源:http://www.ido321.com/1214.html 前两天翻译了一篇文章,关于利用css的border属性制作基本图形:http://www.ido321.com/1200.html ...
- UNIX系统文件
密码文件 密码文件又称用户数据库,一般为/etc/passwd,对应的结构为struct passwd,该文件内容大体如下: 描述 passwd字段 用户名 char* pw_name 加密密码 ch ...
- Java相关书籍推荐
Java从入门到精通(第3版 附光盘) 作 者 明日科技 编 出 版 社 清华大学出版社 出版时间 2012-08-01 版 次 3 页 数 564 印刷时间 2012- ...
- 【Hadoop学习】HDFS 短路本地读
Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146296.html 背景 ...
- 第二百四十天 how can I 坚持
在家待了一天,晚上出去买了个帽子,还有买了点排骨炖着吃了... 玩了好多局游戏. 想搞个直播,不知道能不能玩的起来. 水平太菜了,明天去小米之家玩玩. 睡觉.