您的位置:首页 > 其它

uva 10562 DFS

2015-08-08 10:36 274 查看
#include<bits/stdc++.h>
using namespace std;
const int maxn=210;
int n;
char buf[maxn][maxn];
void dfs(int r,int c)
{
    cout<<buf[r][c]<<'(';
    if(r+1<n&&buf[r+1][c]=='|')
    {
        int i=c;
        while(i-1>=0&&buf[r+2][i-1]=='-') i--;
        while(buf[r+2][i]=='-'&&buf[r+3][i]!='\0')
        {if(!isspace(buf[r+3][i])) dfs(r+3,i);i++;}
    }
    cout<<')';
}
void solve()
{
    n=0;
    for(;;)
    {
        fgets(buf
,maxn,stdin);
        if(buf
[0]=='#')
            break;
        else n++;
    }
    cout<<'(';
    if(n)
    {
        for(int i=0;i<strlen(buf[0]);i++)
        if(buf[0][i]!=' ') {dfs(0,i);break;}
    }
    cout<<')'<<endl;
}
int main()
{
    int T;
    fgets(buf[0],maxn,stdin);
    sscanf(buf[0],"%d",&T);
    while(T--) solve();
    return 0;
}

就是刘汝佳树上的代码

他用了比较少见的fgets和sscanf

fgets 多了一个参数,file * ,填上stdin就和gets相仿了

sscanf 从字符串中读取字符,第一个参数填上字符串指针就可以了

这道题的dfs很简单,就是不断地对字符往下搜索,往下时把前括号打上,退回时把后括号打上 ,就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: