// uva 10562 - Undraw the Trees
// 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=1503
// 题目大意:给出一棵树的图,把他翻译为括号的形式
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int cnt_case;
char map[300][300];
int row;
void FindTree(int, int);
bool Judge(char);
void Init();
int main(){
cin >> cnt_case;
getchar();
while(cnt_case--){
Init();
while(gets(map[row])){
if(strcmp(map[row], "#") == 0)
break;
++row;
}
int column = strlen(map[0]);
if(row){
for(int i = 0; i < column; i++)
if(Judge(map[0][i])){
cout << '(' ;
FindTree(0, i);
cout << ')';
}
} else {
cout << "()";
}
cout << endl;
}
return 0;
}
void Init(){
memset(map, 0, sizeof(map));
row = 0;
}
bool Judge(char ch){
return ch != ' ' && ch != '|' && ch != '-' && ch != 0;
}
void FindTree(int pos_x, int pos_y){
cout << map[pos_x][pos_y];
cout << '(' ;
if(pos_x < row && map[pos_x + 1][pos_y] == '|'){
int column = strlen(map[pos_x + 2]);
int left;
for(int i = pos_y; i >= 0 && map[pos_x + 2][i] == '-'; i--){
left = i;
}
for(int i = left; i < column && map[pos_x + 2][i] == '-'; i++){
if(Judge(map[pos_x + 3][i])){
FindTree(pos_x + 3, i);
}
}
}
cout << ')';
}
uva 10562 - Undraw the Trees
最新推荐文章于 2020-10-07 17:17:28 发布