2011年8月12日

DFS BFS 总结

摘要: 这两三天做了不少DFS,BFS题。。首先是BFS。。1001.赤裸裸的BFS....注意标记。。否则会MLE,RUNTIME ERROR。。BFS要占用非常多的内存。。不断入队。而DFS。。则容易TLE,需要根据题意剪枝,回溯。1002 诡异的楼。变换了下。。电梯方向可以根据时间的奇偶性来判断。同是要用优先队列。。按时间从小到大排序。。1003 knight moves..非常裸的BFS。。8个方向。处理好输入就没什么问题啦1004 asteriods.. 三维空间的BFS。。6个方向。没有什么变化。1005 rescue..刚开始一看题,感觉非常简单。一交wa,才明白朋友可能有多个。。应该从 阅读全文

posted @ 2011-08-12 20:37 more think, more gains 阅读(523) 评论(0) 推荐(0) 编辑

递归

摘要: 输入4398,然后输出‘4’ ‘3’ ’9‘ ’8‘。。。。可以利用递归的性质#include <stdio.h>#include <string.h>#include <stdlib.h>void fun(int x ){ if (x == 0) { return ; } else fun(x / 10); printf("%c\n",x % 10 + '0');}int main( ){ int a, b; while (scanf("%d",&a) != EOF) { fun(a); } 阅读全文

posted @ 2011-08-12 18:47 more think, more gains 阅读(161) 评论(0) 推荐(0) 编辑

Zipper hdu 1501

摘要: 这题首先就把题意理解错啦。。然后就把问题搞的相当复杂。。这题的关键是记忆化。。对以前已经搜索过的状态标记下。。剪枝。还有就是判断DFS结束条件和yes, no...#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>char ch1[410], ch2[410], ch3[500], flag;int len1, len2, len;int hash[410][410];void DFS(int n, int l1, int l2){ if (fl 阅读全文

posted @ 2011-08-12 17:22 more think, more gains 阅读(135) 评论(0) 推荐(0) 编辑

hdu 1572 下沙小面的(2)

摘要: 这题和necklace 类似。。#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;int map[40][40];int dis[40];int hash[40];int N, M, k, sum;void DFS( int S, int n, int s){ int j; if (n == k) { if (sum > S) sum = S; return ; } for(j = 0; j < 阅读全文

posted @ 2011-08-12 10:57 more think, more gains 阅读(301) 评论(0) 推荐(0) 编辑

变形课

摘要: 这题我的思路是。。把字符串转换为矩阵。。然后我写dfs时。。有个条件。。if ( i == 'm' -a') return 1;最后老是错误。。深搜是有很多层。。调用了很多次函数。。 return 1只会结束当前调用的函数。。写有返回值的递归函数要小心啊。最后改成 void 型。。AC 了。。#include <stdio.h>#include <string.h>#include <stdlib.h>int map[1001][1001];int hash[1001][1001];const int inf = 0x7f7f7f7f 阅读全文

posted @ 2011-08-12 09:42 more think, more gains 阅读(261) 评论(0) 推荐(0) 编辑

导航