上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 23 下一页
摘要: 经典的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 阅读(327) 评论(0) 推荐(0) 编辑
摘要: 典型的搜索题目,将1,2,3,4...8填入上图A,B,C...H中,相邻的字母中的两个数不能相邻。现给定一种初始状态,求能否完成上述要求,若方案唯一,则依次输出完成后A,B,C...H中的数。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #define N 9 5 using namespace std; 6 vector<int> g[N]; 7 int a[N],vis[N],ans[N]; 8 void init() 9 {10 g[1 阅读全文
posted @ 2012-05-12 19:41 BeatLJ 阅读(291) 评论(0) 推荐(0) 编辑
摘要: 水题……View Code 1 #include <stdio.h> 2 #include <string.h> 3 #define N 11 4 char g[N][N]; 5 int t[N][N],n,m,k,cnt; 6 int dx[128],dy[128]; 7 int main() 8 { 9 int i,j;10 dx['E']=0,dx['W']=0;11 dx['S']=1,dx['N']=-1;12 dy['E']=1,dy['W']=-1;13 dy[ 阅读全文
posted @ 2012-05-11 23:57 BeatLJ 阅读(181) 评论(0) 推荐(0) 编辑
摘要: 简单bfs题。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #define N 150000 5 using namespace std; 6 queue<int>q; 7 int n,k,t[N]; 8 void bfs() 9 {10 int i;11 memset(t,-1,sizeof(t));12 while(!q.empty()) q.pop();13 t[n]=0;14 q.push(n);15 while(!q.empty())1 阅读全文
posted @ 2012-05-11 22:57 BeatLJ 阅读(252) 评论(0) 推荐(0) 编辑
摘要: 简单搜索题。View Code 1 #include <stdio.h> 2 #define N 100 3 int dx[4]={0,0,1,-1}; 4 int dy[4]={1,-1,0,0}; 5 char g[N][N],n,m,cnt; 6 void dfs(int i,int j) 7 { 8 int ni,nj,d; 9 for(d=0;d<4;d++)10 {11 ni=i+dx[d];12 nj=j+dy[d];13 if(ni<0 || nj<0 || ni>=n || nj>=m || g[ni][nj]!='#' 阅读全文
posted @ 2012-05-11 22:11 BeatLJ 阅读(201) 评论(0) 推荐(0) 编辑
摘要: 简单搜索题。将能走到的地方走一下即可。View Code 1 #include <stdio.h> 2 #define N 20 3 using namespace std; 4 int dx[4]={0,0,1,-1}; 5 int dy[4]={1,-1,0,0}; 6 char g[N][N]; 7 int n,m,cnt; 8 void dfs(int i,int j) 9 {10 int ni,nj,d;11 for(d=0;d<4;d++)12 {13 ni=i+dx[d];14 nj=j+dy[d];15 if(ni<0 || nj<0 ||... 阅读全文
posted @ 2012-05-11 21:54 BeatLJ 阅读(168) 评论(0) 推荐(0) 编辑
摘要: 题目大意:给定一个1-n的排列,规定一种操作,每次只能交换相邻的两个数,求至少进行多少次操作,能将给定的初始态转为目标态,目标态定义为序列中除1外任何一个数都比其左边相邻的那个数大(如果存在),1的左边相邻的数只能是n或者没有。分析:先考虑简单的情况,将初始态转为1,2,3...n。注意到1,2,3...n的逆序数为0,对任何一个序列进行一次上述操作逆序数会增1或减1,如果能保证每次操作都使逆序数减1,那么最少的操作次数便是逆序数,事实正是如此,下面简单证明:当一个序列的逆序数大于0时,则必存在a,b使得a在b的左边且a>b,如果a和b相邻,则交换a,b即可,如果a和b不相邻,则必存在c 阅读全文
posted @ 2012-05-11 19:04 BeatLJ 阅读(251) 评论(0) 推荐(0) 编辑
摘要: 题目链接多维动态规划好题题目大意:给定一个5行9列的中国象棋棋盘(一半),棋盘上红方只剩一个已过河的卒,现黑方让红方连续走k步,求红方最多能吃掉黑方多少棋子。k<=100分析:直接搜索的话肯定会超时(指数级别的复杂度),可以考虑根据卒的位置和其他棋子是否被吃掉来设计状态进行动态规划,关键在于状态的设计,因为卒只能向前冲不能后退,所以后面的行不影响结果,而前面的行中的棋子仍处于初始状态,只有当前行中的棋子的状态需要保存,可以用dp[i][j][k][left][right]表示卒位于i行j列还可以走k步,当前行最左走到left,最右走到right时最多能吃掉多少棋子,状态设计出来了,状态转 阅读全文
posted @ 2012-05-11 16:15 BeatLJ 阅读(302) 评论(0) 推荐(0) 编辑
上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 23 下一页