Test instructions
The relationship of the tree is given in the form of a string
Analysis:
Direct DFS Search, line I, J if it is a letter, Judge I+1 Line J is ' | ' If you find the first '-' in line i+2, find the letter in line I+3 and repeat.
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace Std;
const int maxn=210;
int n;
Char A[MAXN][MAXN];
void Dfs (int r,int c)
{
printf ("%c (", a[r][c]);
if (r+1<n&&a[r+1][c]== ' | ')
{
int i=c;
while (i-1>=0&&a[r+2][i-1]== '-')
i--;
while (a[r+2][i]== '-' &&a[r+3][i]!= ')
{
if (!isspace (A[r+3][i]))
DFS (r+3,i);
i++;
}
}
printf (")");
}
void Solve ()
{
n=0;
while (1)
{
Gets (A[n]);
if (a[n][0]== ' # ')
Break
Else
n++;
}
printf ("(");
if (n)
{
for (int i=0;i<=strlen (a[0]); i++)
{
if (a[0][i]!= ")
{
DFS (0,i);
Break
}
}
}
printf (") \ n");
}
int main ()
{
int T;
scanf ("%d", &t);
GetChar ();
while (t--)
{
Solve ();
}
}
UVa 10562 Undraw the Trees