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 }
  • 相关阅读:
    算法习题---3.11换抵挡装置(UVa1588)
    这不是bug,而是语言特性
    Makefile 11——支持头文件目录指定
    Makefile 10——打造更专业的编译环境-huge项目
    Makefile 9——为依赖关系文件建立依赖关系
    Makefile 8——使用依赖关系文件
    FreeRTOS——1
    Makefile 7——自动生成依赖关系 三颗星
    Makefile学习之路6——让编译环境更加有序
    RCC—使用 HSE/HSI 配置时钟 ---时钟树
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9327280.html
Copyright © 2011-2022 走看看