`
249326109
  • 浏览: 53789 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
文章列表
有点dfs的感觉,树是连通的,可以从根节点一直追踪到叶子。有些比如空树的特殊情况要小心。   #include<stdio.h> #include<string.h> #include<ctype.h> #define MAX 210 char tree[MAX][MAX]; int idx; void drawTree(int i, int j) { if (tree[i + 1][j] == '|') { printf("%c", tree[i][j]); tree[i][j] = '-'; ...

uva 839 - Not so Mobile

    博客分类:
  • acm
递归判断,若有不平衡,标记flag,平衡则返回左右子树重量和。   #include<stdio.h> int flag; int isBalanced() { int wl, dl, wr, dr; scanf("%d%d%d%d", &wl, &dl, &wr, &dr); if (!wl) wl = isBalanced(); if (!wr) wr = isBalanced(); if (wl * dl != wr * dr) { flag = 0; re ...
我的做法没有涉及什么语法树,主要是字符串处理,水平略搓写的有点麻烦,题目说忽略空格,youcase比如 a++有可能是a  +  +,导致WA,后来加上了预处理去掉所有空格才AC。   #include<stdio.h> #include<string.h> char tmpStr[200]; char expr[200]; int appeared[26]; int plus[26]; int minus[26]; char afterExp[200]; int val; void print() { printf("Ex ...
根据给出的先序遍历顺序边构造树边统计树叶之和,根节点在大数组中间索引处开始,左右树依次向两边展开。     #include<stdio.h> #define MAX 1000 int result[MAX]; int cases=1; void printLeaves() { printf("Case %d:\n",cases++); int i; for (i = 0; i < MAX; i++) { if (result[i]) { printf("%d", result[i]); ...
 比较简单,4个方向dfs。     #include<stdio.h> #include<string.h> #include<ctype.h> char maze[35][85]; int lines; void printMaze() { int i; for (i = 0; i < lines; i++) { printf("%s\n", maze[i]); } } void dfs(int i, int j) { if (i < 0 || j < 0 || ...

uva 572 - Oil Deposits

    博客分类:
  • acm
八个方向dfs,统计个数。   #include<stdio.h> #define MAX 105 int map[MAX][MAX]; int m, n; void dfs(int i, int j) { if (i < 0 || j < 0 || i > m || j > n) return; if (map[i][j] == 0) return; map[i][j] = 0; dfs(i - 1, j - 1); dfs(i - 1, j); dfs(i - 1, j + 1); df ...

uva 657 - The die is cast

    博客分类:
  • acm
需要两层dfs,第一层找骰子,第二层在骰子里找点数。递归时访问过的节点记得标记用以结束递归。     #include<stdio.h> #include<stdlib.h> char map[55][55]; int w, h; int count; int num; int arr[1000]; int idx = 1; void subDfs(int i, int j) { if (i < 0 || j < 0 || i >= h || j >= w) return; if (map[i][j ...

uva 712 - S-Trees

    博客分类:
  • acm
题目有点长,但是思路很简单,找出每次的路径2进制序列然后在叶子里找到对应的0或1值即可。   #include<stdio.h> #include<stdlib.h> #include<math.h> int orders[10]; char leaves[200]; int num=1; int main() { int n; char str[10]; while (scanf("%d", &n) != EOF) { if (n == 0) break; printf(&qu ...

297 - Quadtrees

    博客分类:
  • acm
简单点实现可以不用把各个节点连接起来建树,递归的扫描一遍,根据每个节点的表示的跨度范围和类型填充一个1024的数组,每个case中两个树依次填充完毕,最后统计数组中1的个数即为结果。     #include<stdio.h> char str[2000]; int num[1024]; int idx; void buildTree(char *s, int start, int end) { char type = s[idx++]; if (type == 'p') { int mid = (start + end) / 2; int ...

uva 548 - Tree

    博客分类:
  • acm
利用中序和后序构建二叉树然后进行dfs搜索,两步可以同时进行,另外这道题目输入处理也有点麻烦,参考了一些大神的代码,自己重写因为一个值每次case之后未初始化悲剧的RE了好多次。   查了下 runtime error的可能大概有: Runtime Error(ARRAY_BOUNDS_EXCEEDED) // array bounds exceed     数组越界Runtime Error(DIVIDE_BY_ZERO) //divisor is nil                                   除零Runtime Error(ACCESS_VIOLATIO ...

uva 112

    博客分类:
  • acm
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48   寻找是否存在某个从根到叶子的路程和等于某个数值,看了好多人写的,汇总整理了下写了个递归的方法,PKU上1145提交能够AC,UVA上怎么都不行。。后来发现有人给的case里有负数,而且符号和数字之间还有空格。。。蛋疼地增加了处理还是不AC, UVA toolkit上试了几组数据,发现好像有问题,就不折腾了。       #include<stdio.h&g ...

uva 10152 - ShellSort

    博客分类:
  • acm
题目的名字好有爱,本以为是希尔排序,结果是龟壳排序,哈哈。 思路:从后向前遍历对比两组数据,找到在 要求序列中和原序列有相同顺序的元素,要求序列中剩下的继续按照倒序输出就是要在原序列依次选出置顶的。     #include<stdio.h> #include<string.h> int k, n; char origin[205][85]; char required[205][85]; int main() { /* setbuf(stdout,NULL);*/ scanf("%d", &k); w ...

uva 133 - The Dole Queue

    博客分类:
  • acm
题目不难,属于模拟类型的题目,需要比较仔细地处理各种边界情况。估计题目的本意是想让我们实现一个双向循环链表,我看了下N的值最大20,所以想先用数组实现试试,基本思路也是在数组里双向循环,删除的元素标记下;双向循环链表实现就比较好理解了,但是实现起来有些麻烦,指针神马的很容易搞错,需要非常仔细。 开始提交超时了,原因分析了下是因为:虽然N的值不大,但是k和m可以很大,所以找下k/m个的时候需要mod一下当前queue的size。   数组实现: #include<stdio.h> #define MAX 30 /* * status 0 means normal ...
  题目不难,模拟类的问题,用C语言写的。   有些细节要注意,比如各种非法命令如何处理,题目中有明确说明: Any command in which a = b or in which a and b are in the same stack of blocks is an illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks   还有貌似我用ANSI C提交不支持 // 注释,会导致编译错误。   #i ...
The C programming language中一个简单的关于命令行参数的解析实例 #include <stdio.h> #include <string.h> #define MAXLINE 1000 int getline(char *line, int max); #define MAXLINE 1000 int getline(char *line, int max); int getline(char s[], int lim) { int c, i; i = 0; while (--lim > 0 && ...
Global site tag (gtag.js) - Google Analytics