NYoj 素数环(深搜入门)

题目链接:

http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488

深搜模板:

 1 void dfs(int 当前状态)
 2     {
 3           if(当前状态为边界状态)
 4           {
 5             记录或输出
 6             return;
 7           }
 8           for(i=0;i<n;i++)        //横向遍历解答树所有子节点
 9          {
10                //扩展出一个子状态。
11                修改了全局变量
12                if(子状态满足约束条件)
13                 {
14                   dfs(子状态)
15                }
16                 恢复全局变量//回溯部分
17             }
18     }

未优化的代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std;
 8
 9 int a[21]={1};
10 bool visit[21];
11 int n;
12
13 bool isprime(int x){
14   int i;
15   for(i=2;i*i<=x;i++){
16     if(x%i==0)
17       return false;
18   }
19   return true;
20 }
21
22 void DFS(int x){
23   int i;
24   if(x==n-1){
25     if(isprime(a[x]+1)){
26       printf("1");
27       for(i=1;i<n;i++)
28         printf(" %d",a[i]);
29       printf("\n");
30       return ;
31     }
32   }
33   for(i=2;i<=n;i++){
34     if(visit[i]==0&&isprime(a[x]+i)){
35       visit[i]=1;
36       a[x+1]=i;
37       DFS(x+1);
38       visit[i]=0;
39     }
40   }
41 }
42 int main()
43 {
44   int i;
45   int Case=1;
46   while(scanf("%d",&n),n){
47      memset(visit,0,sizeof(visit));
48      printf("Case %d:\n",Case++);
49      if(n%2==0||n==1)
50         DFS(0);
51      else
52         printf("No Answer\n");
53   }
54   return 0;
55 }

素数可以打表:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 bool sushu[]={0,
 5 0,1,1,0,1,0,1,0,
 6 0,0,1,0,1,0,0,0,
 7 1,0,1,0,0,0,1,0,
 8 0,0,0,0,1,0,1,0,
 9 0,0,0,0,1,0,1,0
10 };
11 int a[21], res[21], n, flag;
12 void dfs(int now)
13 {
14   int i;
15   if (now==n&&sushu[a[n-1]+a[n]])
16   {
17     flag = 0;
18     for (i = 0; i<n; i++)
19       cout<<a[i]<<" ";
20     cout<<endl;
21   }
22   else
23   {
24     for (i = 2; i<=n; i++)
25       if (!res[i]&&sushu[i+a[now-1]])
26       {
27         res[i] = 1;
28         a[now] = i;
29         dfs(now+1);
30         res[i] = 0;
31       }
32   }
33 }
34 int main()
35 {
36   int N;
37   N=1;
38   while (cin>>n&&n)
39   {
40     flag = 1;
41     a[0]=a[n]=1;
42     cout<<"Case "<<N++<<":"<<endl;
43     if ((n-1)&1||n==1)
44       dfs(1);
45     if (flag)
46       cout<<"No Answer\n";
47   }
48   return 0;
49 }        

时间: 2024-08-06 02:42:07

NYoj 素数环(深搜入门)的相关文章

hdu1010-Tempter of the Bone DFS深搜入门题+奇偶剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 69699    Accepted Submission(s): 19176 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

NYOJ 10 skiing (深搜和动归)

skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每一个数字代表点的高度.以下是一个样例 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9

POJ 2386 DFS深搜入门

Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains eithe

深搜入门

问题描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different d

nyoj 488 素数环(深搜)

素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输入 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束. 输出 每组第一行输出对应的Case序号,从1开始. 如果存在满足题意叙述的素数环,从小到大输出. 否则输出No Answer. 样例输入 6 8 3 0 样

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

NYOJ 迷宫寻宝(一)深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 做这道题的时候迷迷糊糊的,,果然比较难..最后也是没有做出来..请教了一下学长,学长说我基础还不好..基础果然重要,这道题是一道搜索题,我没有考虑钥匙在门后面的情况,比如aBbSAG 多亏学长指教,通过这道题对深搜的理解又加深了一步~ #include <iostream> #include <cstdio> #include <cstring> using

NYOJ 293 Sticks 【深搜】+【剪枝】

这是一道让人泪奔的题,它深刻的说明了什么是剪枝,哪怕是再小的一个细节,一旦递归规模增大都会引发巨大的时间消耗,真是神题~ Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the

poj 1164 The Castle (入门深搜)

题目: 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | # # # # # #---#####---#####---#####---# 3 # | | # # # # # #---#########---#####---#---# 4 # # | | | | # # ############################# (Figure 1)