zoukankan      html  css  js  c++  java
  • UVa10562 Undraw the Trees

     

    注意点:

    空树情况处理。

    循环时候可以先判断符合条件,再递减:

    while(i-1>=0 && buf[r+2][i-1]=='-') i--;

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int N=200+2;
    char buf[N][N];
    int n;
    
    //递归遍历并且输出以字符buf[r][c]为根的树
    void dfs(int r, int c)
    {
        printf("%c(", buf[r][c]);
        //看看是否有子节点
        if(r+1<n && buf[r+1][c]=='|')
        {
            //找最左边界i
            int i=c;
            while(i-1>=0 && buf[r+2][i-1]=='-') i--;
            while(buf[r+2][i]=='-' && buf[r+3][i]!=0)
            {
                if(!isspace(buf[r+3][i]))
                        dfs(r+3, i);
                i++;
            }
        }
        printf(")");
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("./uva10562.in", "r", stdin);
    #endif
        int T;
        gets(buf[0]);
        sscanf(buf[0], "%d", &T);
        while(T--)
        {
            n=0;
            while(1)
            {
                gets(buf[n]);
                if(buf[n][0]=='#')
                    break;
                n++;
            }
    
            printf("(");
            //注意处理空树
            if(n)
            {
                for(int i=0;i<strlen(buf[0]);i++)
                {
                    if(buf[0][i]!=' ')
                    {
                        dfs(0, i);
                    }
                }
            }
            printf(")
    ");
        }
        
    
        return 0;
    }

     

    // UVa10562 Undraw the Trees
    // Rujia Liu
    // 题意:把画得挺好看的多叉树转化为括号表示法
    // 算法:直接在二维字符数组里递归。注意空树,并且结点标号可以是任意可打印字符
    
    #include<cstdio>
    #include<cctype>
    #include<cstring>
    using namespace std;
    
    const int maxn = 200 + 10;
    int n;
    char buf[maxn][maxn];
    
    // 递归遍历并且输出以字符buf[r][c]为根的树
    void dfs(int r, int c) {
      printf("%c(", buf[r][c]);
      if(r+1 < n && buf[r+1][c] == '|') { // 有子树
        int i = c;
        while(i-1 >= 0 && buf[r+2][i-1] == '-') i--; // 找"----"的左边界
        while(buf[r+2][i] == '-' && buf[r+3][i] != '') {
          if(!isspace(buf[r+3][i])) dfs(r+3, i); // fgets读入的'
    '也满足isspace()
          i++;
        }
      }
      printf(")");
    }
    
    void solve() {
      n = 0;
      for(;;) {
        fgets(buf[n], maxn, stdin);
        if(buf[n][0] == '#') break; else n++;
      }
      printf("(");
      if(n) {
        for(int i = 0; i < strlen(buf[0]); i++)
          if(buf[0][i] != ' ') { dfs(0, i); break; }
      }
      printf(")
    ");
    }
    
    int main() {
      int T;
      fgets(buf[0], maxn, stdin);
      sscanf(buf[0], "%d", &T);
      while(T--) solve();
      return 0;
    }
  • 相关阅读:
    TS 3.1
    TS 3.1
    MDN 教程
    MDN 教程
    MDN 教程
    MDN 教程
    MDN 教程
    MDN 教程
    cookie会话技术
    数据库语法-1
  • 原文地址:https://www.cnblogs.com/cute/p/3645462.html
Copyright © 2011-2022 走看看