您的位置:首页 > 其它

UVa 10562 - Undraw the Trees ( DFS )

2018-03-06 12:43 501 查看

题意

看图写树

思路

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] != '\0'){
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
, maxn, stdin );
if( tre
[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(")\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva DFS