摘要:
经典的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]=. 阅读全文
摘要:
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, 阅读全文
摘要:
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 阅读全文