Undraw The Trees
Abstract: The tree represented by a two-dimensional character array, represented by a one-dimensional array of characters.
Idea: recursively draw trees directly in a two-dimensional character array.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 Const intms=205;8 9 CharStr[ms][ms];Ten intN; One voidDrawintRintc) A { -printf"%c (", Str[r][c]); - if(r+1<n&&str[r+1][c]=='|') the { - intj=C; - while(J-1>=0&&str[r+2][j-1]=='-') -j--; + for(inti=j;str[r+2][i]=='-'&&str[r+3][i]!='\ n'; i++) -{//special attention is needed here str[r+3][i]!= ' \ n ' + if(str[r+3][i]!=' ')//consider this situation str[r+2][i]== '-' &&str[r+3][i]== ' \ n ' A { atDraw (r+3, i); - } - } - } -printf")"); - } in - intMain () to { + intT; -scanf"%d",&T); theGetChar ();//Fgets will read into line breaks * while(t--) $ {Panax Notoginsengn=0; - while(1) the { + fgets (Str[n],ms,stdin); A if(str[n][0]=='#') the Break; +n++; - } $printf"("); $ if(n) - { - for(intI=0; str[0][i]!='\ n'; i++) the if(str[0][i]!=' ') - {WuyiDraw0, i); the Break; - } Wu } -printf") \ n"); About } $ return 0; -}
Undraw the Trees string array recursive drawing tree