uva 10562 - Undraw the Trees

// 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 << ')';
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值