摘要: 这一题感觉是深搜的思路比较好想,不过容易超时,必须加些剪枝。广搜的话思路比较难想,代码也挺复杂的,我一直没编对,所以这里只有深搜的代码~剪枝策略:1.首先判断两点是否相等,最基础的剪枝;2.判断起始点是否为0,为0直接输出NO;3.当当前的路线转折数已经到2时,判断当前点与终点的横纵坐标是否至少有一个相等,如果都不相等则剪去;AC code:View Code 1 #include <iostream> 2 #define MAX 1001 3 using namespace std; 4 int n, m; 5 int map[MAX][MAX]; 6 bool vis[MAX] 阅读全文
posted @ 2012-03-25 09:02 背着超人飞 阅读(176) 评论(0) 推荐(0)
摘要: 三维的BFS,很裸的题,不过一定要注意这一题给的坐标x表示列,y表示行,z表示层,实际输入的时候调试一下看看给的坐标是不是跟样例在图中所指的对象一样就行了。AC code:View Code 1 #include <iostream> 2 #define MAX 10000 3 using namespace std; 4 struct node{ 5 int j, i, k, step; 6 }que[MAX]; 7 char s[10]; 8 int n; 9 int sj, si, sk;10 int ej, ei, ek;11 char map[11][11][11];12 阅读全文
posted @ 2012-03-24 21:16 背着超人飞 阅读(426) 评论(0) 推荐(0)
摘要: 这题就是比较水的一道搜索题了,BFS跟DFS都能做,直接看代码吧!AC code:View Code 1 #include <iostream> 2 #define MAX 50 3 using namespace std; 4 int w, h; 5 char map[MAX][MAX]; 6 int dir[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; 7 int count; 8 bool inmap(int x,int y) 9 {10 return x >= 0 && x < h && y 阅读全文
posted @ 2012-03-23 00:23 背着超人飞 阅读(138) 评论(0) 推荐(0)
摘要: 题目大意是找到独立的油田个数,所谓独立就是跟它相邻的八个方向上都不能再有别的油田。思路就是每遇到一个'@'都要将它周围的所有'@'都给改成'*',用DFS跟BFS都可以,这里是DFS的代码:AC code:View Code 1 #include <iostream> 2 #define MAX 101 3 using namespace std; 4 int m, n, count; 5 char map[MAX][MAX]; 6 int dir[8][2] = {{0, 1}, {0, -1}, {1, 0},{-1, 0}, { 阅读全文
posted @ 2012-03-21 20:51 背着超人飞 阅读(140) 评论(0) 推荐(0)
摘要: 这一题是又裸又水的一道BFS题,唯一复杂的地方就是扩展到了三维,但是跟二维的区别不大。直接看代码吧:AC code:View Code 1 #include <iostream> 2 #define MAX 50 3 #define SZ 51 4 using namespace std; 5 struct node{ 6 int x, y, z; 7 int step; 8 }que[MAX*MAX*MAX*2]; 9 int a, b, c, t, flag;10 int map[SZ][SZ][SZ];11 int d[][3]={-1,0,0,1,0,0,0,-1,0... 阅读全文
posted @ 2012-03-21 00:34 背着超人飞 阅读(180) 评论(0) 推荐(0)
摘要: 这一题算是一道典型的裸睡(又裸又水)题了吧,不过用刚学的滚动数组进行了优化,时间跟空间复杂度大幅度下降,如果字符长度超过5000的话恐怕不进行优化是要MLE的,不多说了,看代码吧。。。状态f[i][j]表示检查到Ai跟Bj时的最长公共子序列的长度,如果Ai = Bj,那么f[i][j] = f[i - 1][j - 1] + 1,否则考虑三种情况:(1)Ai在公共子序列中,Bj不在, f[i][j - 1];(2)Bi在公共子序列中,Ai不在, f[i - 1][j];(3)Ai跟Bi都不在,f[i - 1][j - 1]。由于第三种情况一定小于前两种情况,故f[i][j] = max{f.. 阅读全文
posted @ 2012-03-06 16:54 背着超人飞 阅读(179) 评论(0) 推荐(0)
摘要: 这一题的思路就是先求出原序列与原序列的逆序列的最大公共子序列的长度,然后用原序列的长度减去即可。至于为什么这样做还请高手指点。。。因为题目中的数据量比较大,开5000*5000的数组会MLT,所以考虑用滚动数组来做。。。状态f[i][j]表示检测的Ai与Bj时的最大公共子序列的长度,状态转移方程为:f[i][j] = max{f[i - 1][j], f[i][j - 1]} + 1。。。AC code:View Code 1 #include <iostream> 2 #define MAX 5001 3 using namespace std; 4 int maxlen[2][ 阅读全文
posted @ 2012-03-06 16:14 背着超人飞 阅读(140) 评论(0) 推荐(0)
摘要: 这一题就是典型的动态规划,从数塔的底部开始用dp[i][j]表示到第i行第i个数的时候数塔的最大值,转移方程为dp[i][j] = dp[i][j] + max{dp[i + 1][j], dp[i + 1][j + 1]}, 显然最后结果为dp[1][1]。AC code: 1 #include <iostream> 2 #define MAX 500 3 using namespace std; 4 int n; 5 int tri[MAX][MAX]; 6 int d[MAX][MAX]; 7 8 void input() 9 {10 for(int i = 1; i < 阅读全文
posted @ 2012-03-04 22:57 背着超人飞 阅读(122) 评论(0) 推荐(0)
摘要: 这一题就是将一维的最大字段和扩展到二维,在一维的求最大字段和的过程中是这样操作的:int max_sum(int n){ int i, j, sum = 0, max = -10000; for(i = 1; i <= n; i++) { if(sum < 0) sum = 0; sum += a[i]; if(sum > max) max = sum; } return sum;}扩展到二维的时候也是同样的方法,不过需要将二维压缩成一维,所以我们要将数据做一下处理,使得map[... 阅读全文
posted @ 2012-03-04 22:41 背着超人飞 阅读(1608) 评论(1) 推荐(2)
摘要: 这一题是比较简单的动态规划,状态f[i]表示走前i个棋子时的最高分数,状态转移方程为f[i] = {max(f[j]), i > j && step[i] > step[j] + step[i]}.AC code: 1 ude <iostream> 2 #define MAX 1000 3 using namespace std; 4 5 int step[MAX]; 6 int f[MAX]; 7 int n; 8 int max_sum(int i) 9 {10 int max = 0;11 int j;12 for(j = 0; j < i; 阅读全文
posted @ 2012-03-01 21:50 背着超人飞 阅读(254) 评论(0) 推荐(0)