UVA10562(看图写树,dfs)

这个题过的好艰难,不过真的学到好多。

关于fgets的用法真的是精髓。!isspace(c)和c!=' '是有区别的。

其它的看代码吧

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 #include <cctype>
13 
14 using namespace std;
15 const double Pi=3.14159265358979323846;
16 typedef long long ll;
17 const int MAXN=200+5;
18 const int dx[5]={0,0,0,1,-1};
19 const int dy[5]={1,-1,0,0,0};
20 const int INF = 0x3f3f3f3f;
21 const int NINF = 0xc0c0c0c0;
22 const ll mod=1e9+7;
23 char buf[MAXN][MAXN];
24 int n;
25 void dfs(int i,int c)
26 {
27     printf("%c(",buf[i][c]);
28     //准备向下递归 
29     if(i+1<n&&buf[i+1][c]=='|')
30     {
31         int r=c;
32         //很巧妙的地方,只用找到左边界就可以了,以为它一直是有'-'的,所以一定知道右边界在哪 
33         while(r-1>=0&&buf[i+2][r-1]=='-')    {r--;}
34         while(buf[i+2][r]=='-'&&buf[i+3][r]!='\0')
35         {
36             if(!isspace(buf[i+3][r])) dfs(i+3,r);
37             r++;
38         }
39     } 
40     printf(")");
41 }
42 
43 void solve()
44 {
45      n=0;
46     while(1)
47     {
48         fgets(buf[n],MAXN,stdin);
49         if(buf[n][0]=='#') break;
50             else n++;    
51     }
52 
53     printf("(");
54     if(n)
55     {
56         //这一步应该是从根开始递归,因为只有一个根,所以只需要递归一次 
57         for(int i=0;i<strlen(buf[0]);i++)
58         {
59             if(buf[0][i]!=' ') 
60             {
61                 dfs(0,i);
62                 break;
63             } 
64         }
65     }
66     printf(")\n");
67 }
68 
69 int main()
70 {
71     int t;
72     cin>>t;cin.get(); 
73     while(t--)
74     {
75         solve();
76     }
77     return 0;
78 }

 

转载于:https://www.cnblogs.com/Msmw/p/10691062.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值