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 ...
随机推荐
- DomeOS部署
http://gitbook.domeos.org/ 按照官方一键部署脚本完之后,发现监听的IP地址是在IPv6上面. vim /etc/default/grubadd ipv6.disable=1 ...
- MySQL— pymysql模块(防止sql注入),可视化软件Navicat
一.Pymysql import pymysql #python2.X 中是 mysqldb 和 pythonmysql 用法是一模一样的 #pymysql可以伪装成上面这两个模块 user = in ...
- windows下Go升级及GoLand的安装激活
之前在qor/China上问golang项目包有没有一键安装,有位朋友给出了回复: 于是,尝试了下升级Go,目前版本是1.10.1,现升级为为1.12.2.步骤如下: 一.下载安装包 下载地址:htt ...
- React 的坑
MemoryRouter 会缓存组件,导致有时候 componentDidMount 不会执行
- Eclipse在线集成SpringBoot
在线集成下载地址:http://dist.springsource.com/release/TOOLS/update/e4.8/ 注意:需要更改后面的版本号,跟随自己eclipse版本号下载,只需要改 ...
- 庄博园的Mp4
- 二、linux的安装
1. 虚拟机安装: 1.1. 什么是虚拟机 虚拟机:一台虚拟的电脑. 虚拟机软件: * VmWare :收费的. * VirtualBox :免费的. 1.2. 安装VmWare 参考<虚拟软件 ...
- numpy数据集练习
#1. 安装scipy,numpy,sklearn包 import numpy as np #2. 从sklearn包自带的数据集中读出鸢尾花数据集data from sklearn.datasets ...
- 使用junit和eclemma进行简单的代码测试
1.Junit和Hamcrest的安装 可以在https://mvnrepository.com/上面下载所需要的Junit和Hamcrest的jar包,然后在项目中新建一个lib文件夹,将下载好的j ...
- 2017年4月13日用VS写C程序遇到的一些问题
在网上找到一篇展示计算机浮点数格式的文章,且有C代码如下: #include <stdio.h> #include <stdlib.h> #include <string ...