上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 23 下一页
摘要: BFS基础题View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 201 5 #define INF 0x7fffffff 6 using namespace std; 7 queue<int> Q; 8 int d[N],t[N],n,a,b,cur,next; 9 char inq[N];10 void bfs()11 {12 for(int i=1;i<=n;i++) t[i]=INF;13 memset(inq,0,si 阅读全文
posted @ 2012-05-18 23:08 BeatLJ 阅读(237) 评论(0) 推荐(0) 编辑
摘要: 题目大意:给定n个正整数,问能否分成4组,且每组的和相等。分析:这题就是sticks那题的简化版。思路一样。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <vector> 5 #define N 20 6 using namespace std; 7 vector<int> edge[4]; 8 int len[N],n,sum; 9 char vis[N],yes;10 int cmp(const void* 阅读全文
posted @ 2012-05-18 20:14 BeatLJ 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 题目数学模型:给定n个正整数,现要将这n个数分成k组,且满足每组的和都相等。求最多能分多少组。这题是经典的剪枝搜索题,原题来自PKU。下面的程序虽然AC了,但是跑不动POJ中discuss中的那组BT数据。View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <vector> 5 #define MAX(a,b) ((a)>(b)?(a):(b)) 6 #define N 64 7 using namespace std; 阅读全文
posted @ 2012-05-18 17:56 BeatLJ 阅读(366) 评论(0) 推荐(0) 编辑
摘要: 跟"非常可乐"那题差不多,只是多了一个打印倒水步骤。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 1001 5 #define MIN(a,b) ((a)<(b)?(a):(b)) 6 using namespace std; 7 typedef struct node 8 { 9 int v[2],t,d;10 }node;11 node cur,next;12 queue<node> Q;13 n 阅读全文
posted @ 2012-05-18 12:24 BeatLJ 阅读(237) 评论(0) 推荐(0) 编辑
摘要: 题目大意:英雄打怪,英雄有n个咒语,每个咒语能对怪造成一定的伤害,且只能使用一次,咒语使用的时机不同,伤害不同,具体表现为,当怪的体力小于某个特定值m时,伤害会加倍。每条咒语描述为(a,m),表示怪的体力大于m时伤害为a,其他时间为2*a。问能不能将怪打死,若能输出最少使用的咒语数,否则输出"-1"。当怪的体力小于或等于0时即认为怪死了。分析:要求最少的次数,很容易想到使用BFS,但用DFS的关键在于状态的判重,这题可以将已经使用的咒语列表构成状态,判重时不好处理。若使用迭代加深搜索IDS则不需判重,只是时间可能要多一点。因为n最大不超过10,即最大深度为10,肯定不会超时 阅读全文
posted @ 2012-05-18 11:08 BeatLJ 阅读(410) 评论(0) 推荐(0) 编辑
摘要: 题目大意:这题跟HDOJ"非常可乐"那题很像,用状态空间搜索即可。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 101 5 #define MIN(a,b) ((a)<(b)?(a):(b)) 6 using namespace std; 7 typedef struct node 8 { 9 int v[2],t;10 }node;11 node cur,next;12 queue<node> Q; 阅读全文
posted @ 2012-05-17 18:03 BeatLJ 阅读(224) 评论(0) 推荐(0) 编辑
摘要: 题目模型:给定3个无刻度容器,容器的容积均为正整数,初始状态为第一个容器装满水,其它2个空着,问是否能将第一个容器中的水平分,若能输出最少操作步数,否则输出"NO".分析:典型的状态空间搜索题,要求最少步数,可以用BFS,将3个容器中中的水量组合定义为状态,倒水操作会造成状态转移。目标状态为某两个容器中水量相等且总和为总水量。一个小的优化是当总水量为奇数时,直接输出"NO".View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 阅读全文
posted @ 2012-05-17 16:40 BeatLJ 阅读(384) 评论(0) 推荐(0) 编辑
摘要: 题目的模型:在一个图中,给定一系列目标结点,求从起点出发经过所有目标结点的最短距离。分析:由于目标结点数目最大为7,所以可以暴力搜索过,枚举经过目标结点的排列,然后计算选择最优的。计算距离时需要用floyd预处理任意2结点之间的最短距离。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #define N 31 4 #define MIN(a,b) ((a)<(b)?(a):(b)) 5 #define INF 0x7fffffff 6 int d[N][N],t[N],a[N],n,m,cnt,ans; 阅读全文
posted @ 2012-05-17 10:27 BeatLJ 阅读(172) 评论(0) 推荐(0) 编辑
摘要: 题目大意:跟01背包模型有点像,区别在于01背包对选取的物品个数没有要求,而这题给出了选的个数。N最大为20,所以可以暴力过。经测试N最大好像是21,定义成20会WAView Code 1 #include <stdio.h> 2 #define N 21 3 #define INF 0x7fffffff 4 int v[N],w[N],n,m,cnt,vmax,wmax; 5 void dfs(int k,int vsum,int wsum) 6 { 7 if(cnt==m) 8 { 9 if(wsum<=wmax && vsum>vmax) vmax 阅读全文
posted @ 2012-05-16 23:32 BeatLJ 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 题目大意:给定一组单词(均为小写英文字母),每个单词表示能将其首字母变成尾字母,例如"go"表示能将'g'变成'o',问同过给定的单词能否将'b'变成'm'。分析:先建立有向图,然后对图进行DFS。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #define N 60000000 4 char s[N],vis[26]; 5 bool g[26][26]; 6 bool read_case() 7 { 8 int n; 9 阅读全文
posted @ 2012-05-16 19:57 BeatLJ 阅读(197) 评论(0) 推荐(0) 编辑
上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 23 下一页