UVa 10562 - Undraw the Trees//遍历or递归
来源:互联网 发布:dw软件代码 编辑:程序博客网 时间:2024/06/11 04:11
这道题初看输入有点烦,仔细一看就开个二位数组,直接遍历数组求解就ok了。但是输出还有括号,在这里跪了几次。然后值得注意的是,节点不一定是字母,页可以使其他的字符。最后注意边界就ok啦!下面是代码:
#include<stdio.h>#include<string.h>#define MAXN 210char map[MAXN][MAXN];int n;void dfs(int x,int y){ printf("%c(",map[x][y]); if(x==n-1)// out of the recursion,so it is a leaf { printf(")"); return; } if(map[x+1][y]=='|')// it has childs { int i; for(i=y;map[x+2][i]=='-';i--);// find his leftest child i=i+1; for(int j=i;map[x+2][j]=='-'&&j<strlen(map[x+3]);j++) { if(map[x+3][j]!=' '&&map[x+3][j]!='\0')// '\0' may be conuted { //printf("("); dfs(x+3,j); } } } printf(")"); return ;}int main(){ int t; scanf("%d",&t); getchar(); while(t--) { n=0; //memset(map,'\0',sizeof(map)); while(gets(map[n])&&map[n][0]!='#') n++; int len=strlen(map[0]); int flag=0; for(int i=0;i<n;i++) { for(int j=0;j<len;j++) if(map[i][j]!=' '){ printf("("); dfs(i,j); printf(")\n"); flag=1; break; } if(flag) break; } if(!flag) printf("()\n"); } return 0;}ps:在n次wa后,很想看大牛代码的,还是忍住啦!
- UVa 10562 - Undraw the Trees//遍历or递归
- 数据结构 递归 uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- uva-10562Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA - 10562 Undraw the Trees
- UVa 10562 - Undraw the Trees
- Uva - 10562 - Undraw the Trees
- 【程序13】打印出所有的“水仙花数”
- RTI1.3时间管理支持的两种模式
- 软件示例中常用foo函数,foo变量来表示,这是为何?
- [career] 3D图形向量基础
- 继承的约束:构造器不可调用被覆盖的方法
- UVa 10562 - Undraw the Trees//遍历or递归
- IT职场规划与学习方法
- 最近一直忙
- android中获取即时天气
- 已知平面上若干个点的坐标。需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。
- 路由器与猫的区别
- Java GUI编程中AWT/swing/SWT的优缺点
- “invalid byte 1 of 1-byte UTF-8 sequence”异常
- Qt整体框架浅析(二) -信号机制