UVa 10562

题意

看图写树

思路

DFS递归

AC代码

(学习紫书)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

const int maxn = 200 + 10;
char tre[maxn][maxn];
int n;

void DFS(int x, int y)
{
    printf("%c(", tre[x][y]);
    if( x+1<n && tre[x+1][y] == '|')
    {
        int i = y;
        while(i-1 >= 0 && tre[x+2][i-1] == '-')
            i--;
        while(tre[x+2][i] == '-' && tre[x+3][i] != ''){
            if(!isspace(tre[x+3][i]))
                DFS(x+3, i);
            i++;
        }
    }
    printf(")");
}
void solve();

int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
        solve();
    return 0;
}

void solve()
{
    n = 0;
    for(;;){
        fgets( tre[n], maxn, stdin );
        if( tre[n][0] == '#' )  break;
        else    n++;
    }
    printf("(");
    if( n ){
        for( int i = 0; i < strlen(tre[0]); i++ ){
            if(tre[0][i] != ' '){
                DFS(0, i);
                break;
            }
        }
    }
    printf(")
");
}
原文地址:https://www.cnblogs.com/JinxiSui/p/9740597.html