zoukankan      html  css  js  c++  java
  • UVa 10562 Undraw the Trees(递归遍历)

    题目链接:

    https://cn.vjudge.net/problem/UVA-10562

    Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don't know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor's works were found in a 3.5" floppy disk left inside the drive.

    The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek -- we leave this trivial task for you. Convert professor's trees to our trees.

    Professor's Trees

    The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 <= T <= 20)drawn in the file. Then you would have T trees, each ending with a single hash ('#') mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols '-''|'' ' (space) and '#'. Every node that has children has a '|' symbol drawn just below itself. And in the next line there would be a series of '-' marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.

    Our Trees

    Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.

     1 /*
     2 题意描述: 
     3 给出一棵多叉树,转化成括号表示法
     4 解题思路:
     5 将图存储再二维数组中,根据规则递归输出,无需建树
     6 易错分析:
     7 注意空树的输出 
     8 */ 
     9 #include<bits/stdc++.h>
    10 
    11 const int maxn=200+10;
    12 int n;
    13 char buf[maxn][maxn];
    14 void dfs(int r,int c);
    15 int main()
    16 {
    17     int T;
    18     //freopen("testin.txt","r",stdin);
    19     scanf("%d",&T);
    20     getchar();//gets前吃掉换行符 
    21     while(T--){
    22         memset(buf,0,sizeof(maxn*maxn));
    23         n=0; 
    24         while(1){
    25             gets(buf[n]);
    26             if(buf[n][0] == '#')
    27                 break;
    28             else
    29                 n++;
    30         }    
    31         /*for(int i=0;i<n;i++)
    32             puts(buf[i]);*/
    33         printf("(");
    34         if(n){//防止空树 
    35             for(int i=0;i<strlen(buf[0]);i++){
    36                 if(buf[0][i] != ' '){
    37                     dfs(0,i);
    38                     break;
    39                 } 
    40             }
    41         }
    42         printf(")
    "); 
    43     }
    44     return 0;    
    45 } 
    46 //递归遍历并输出以buf[r][c]为根节点的树 
    47 void dfs(int r,int c){
    48     printf("%c(",buf[r][c]);
    49     if(r+1 < n && buf[r+1][c] == '|'){//有子树 
    50         int i=c;
    51         while(i-1 >= 0 && buf[r+2][i-1] == '-') i--;
    52         while(buf[r+2][i] == '-' &&  buf[r+3][i] != ''){
    53             if(buf[r+3][i] != ' ')
    54                 dfs(r+3,i);
    55             i++;
    56         }
    57     } 
    58     printf(")"); 
    59 }
  • 相关阅读:
    JDBC中的PreparedStatement相比Statement的好处
    说出一些数据库优化方面的经验?
    数据库三范式是什么?
    用jdom解析xml文件时如何解决中文问题?如何解析?
    我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
    MVC的各个部分都有那些技术来实现?如何实现?
    JSP和Servlet有哪些相同点和不同点,他们之间的联系是什么?
    黑盒测试和白盒测试是软件测试的两种基本方法,请分别说明各自的优点和缺点!  
    串行(serial)收集器和吞吐量(throughput)收集器的区别是什么?
    说几个常见的编译时异常类?
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9327280.html
Copyright © 2011-2022 走看看