UVA10562(看图写树,dfs)
这个题过的好艰难,不过真的学到好多。
关于fgets的用法真的是精髓。!isspace(c)和c!=' '是有区别的。
其它的看代码吧
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype> using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
const int dx[]={,,,,-};
const int dy[]={,-,,,};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
char buf[MAXN][MAXN];
int n;
void dfs(int i,int c)
{
printf("%c(",buf[i][c]);
//准备向下递归
if(i+<n&&buf[i+][c]=='|')
{
int r=c;
//很巧妙的地方,只用找到左边界就可以了,以为它一直是有'-'的,所以一定知道右边界在哪
while(r->=&&buf[i+][r-]=='-') {r--;}
while(buf[i+][r]=='-'&&buf[i+][r]!='\0')
{
if(!isspace(buf[i+][r])) dfs(i+,r);
r++;
}
}
printf(")");
} void solve()
{
n=;
while()
{
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;
cin>>t;cin.get();
while(t--)
{
solve();
}
return ;
}
UVA10562(看图写树,dfs)的更多相关文章
- 6-17 看图写树 uva10562
非常好的dfs题 有很多细节 关于‘ ’ ‘0’ ’\n‘ 的处理 他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...
- 看图写树 (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看图写树(二叉树遍历)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Uva 10562 看图写树
题目链接:https://uva.onlinejudge.org/external/105/10562.pdf 紫书P170 直接在二维数组上做DFS,用的fgets函数读入数据,比较gets函数安全 ...
- 看图写代码---看图写代码 阅读<<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 ...
- 看图轻松理解数据结构与算法系列(NoSQL存储-LSM树) - 全文
<看图轻松理解数据结构和算法>,主要使用图片来描述常见的数据结构和算法,轻松阅读并理解掌握.本系列包括各种堆.各种队列.各种列表.各种树.各种图.各种排序等等几十篇的样子. 关于LSM树 ...
- SDRAM的初始化与刷新操作---看时序图写代码
SDRAM的初始化与刷新操作---看时序图写代码 1.SDRAM的常见操作 2.初始化就是配置SDRAM 3.SDRAM初始化时序 时序解释如下: 4.刷新操作
- HDU 5692 线段树+dfs序
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- Ubuntu18.04下给PyCharm创建快捷方式
Ubuntu18.04下给PyCharm创建快捷方式 该方法 WebStorm.PyCharm.Clion 等都适用. 步骤 终端输入: sudo gedit /usr/share/applicati ...
- 使用idea搭建maven项目,结果spring-mybatis.xml文件报红“Cannot resolve file 'jdbc.properties' less... (Ctrl+F1) Inspection info:Spring XML model validation”
错误如下图: 解决方案: 暂时只是使用右下角的highlightinglevel来屏蔽,拖动到最左边就行了
- Springmvc对就jdbc封装的操作
解释: JdbcTemplate 封装常用的JDBC方法 HibernateTemplate 封装常用Hibernate方法 JdbcDaoSupport JDBC数据访问对象的基类 Hibern ...
- linux服务器用nginx做网站内页之间301的跳转方法
例: 要将这个页面 /topic/show-228-1.html 做301跳转到 /dance/topic-show-228-1.html nginx的伪静态规则就这样写: rewrite ^/top ...
- Saiku Table展示数据合并bug修复(二十五)
Saiku Table展示数据合并bug修复 Saiku以table的形式展示数据,如果点击了 非空的字段 按钮,则会自动进行数据合并,为空的数据行以及数据列都会自动隐藏掉. 首先我们应该定位问题: ...
- linux 迁移项目ProtocolException
背景:服务器跟换机房,虚拟机完整迁移项目,只修改ip和主机名 1.检查/etc/hosts 中ip 和主机名映射 2.检查网络端口是否有限制以及端口开放是否全了,检查ip有没有配对.RMI注册不上.
- python笔记23-模块导入、安装
模块:import 模块的实质就是把要导入模块里面的代码,从上到下执行一遍,找模块的顺序是,先从当前目录下找,找不到的话,再环境变量里面找 一:导入模块 1.查找环境变量地址: import sysp ...
- PAT A1046 Shortest Distance
PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...
- Python 字符串String相关知识
test.capitalize( ) |首字母大写 test.lower( ) |全部变成小写(只能处理英文字母) test.casefold( ) | ...
- Java 错误提示org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
java 操作excel文件 发布后报错 org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException XSS ...