摘要: 题目大意:跟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) 编辑
摘要: 经典的DFS,素数环问题。题目大意:将从1开始的前n个自然数排成一个圈,使得任意相邻的两个数的和是素数。给定n,按字典序打印结果。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #define N 20 4 int a[N],n,cnt; 5 char is_p[40],vis[N]; 6 void init() 7 { 8 memset(is_p,0,sizeof(is_p)); 9 is_p[2]=1;10 is_p[3]=1;11 is_p[5]=1;12 is_p[7]=1;13 is_p[11]=. 阅读全文
posted @ 2012-05-16 19:25 BeatLJ 阅读(175) 评论(0) 推荐(0) 编辑
摘要: BFS题,走三维迷宫。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 50 5 using namespace std; 6 typedef struct node 7 { 8 int x,y,z; 9 }node;10 queue<node> Q;11 node cur,next;12 int dx[6]={1,-1,0,0,0,0};13 int dy[6]={0,0,1,-1,0,0};14 int dz[6]={0,0, 阅读全文
posted @ 2012-05-16 17:21 BeatLJ 阅读(201) 评论(0) 推荐(0) 编辑
摘要: BFS题,数学模型如下:对于一个01序列(长度小于20),定义翻转操作为:选定序列中的一位,将其取反,并将其左右相邻的位(如果存在)取反,现给定一个初始序列,求最少需操作多少次使得序列变成全0序列。分析:序列状态可用一个32位整数表示,状态数目最多为220,所以搜索不会超时,翻转操作可用异或运算来表示。需注意的是,写好后别忘了测试n=1的情况。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <queue> 5 using nam 阅读全文
posted @ 2012-05-16 16:18 BeatLJ 阅读(328) 评论(0) 推荐(0) 编辑