zoukankan      html  css  js  c++  java
  • hdu 1016 Prime Ring Problem(dfs)

    Prime Ring Problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 34799    Accepted Submission(s): 15411


    Problem Description
    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

    Note: the number of first circle should always be 1.

     
    Input
    n (0 < n < 20).
     
    Output
    The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

    You are to write a program that completes above process.

    Print a blank line after each case.
     
    Sample Input
    6
    8
     
    Sample Output
    Case 1:
    1 4 3 2 5 6
    1 6 5 2 3 4
     
     
    Case 2:
    1 2 3 8 5 6 7 4
    1 2 5 8 3 4 7 6
    1 4 7 6 5 8 3 2
    1 6 7 4 3 8 5 2
     
    Source
     
    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1072 1372 1015 1258 1180 
     
    这是我第一道接触的搜索题,当时参考学长的代码写,却始终不能理解代码的意思,于是就一遍一遍第敲,直到整个代码都背下来了,后来接触的搜索逐渐增加,终于理解了这道题的代码,每次看到这道题,就能想起当年苦逼的敲代码。。。
     
    题意:输入一个数n,代表从1到n个数,排序排成一个环,使得相邻两个数之和为素数(第一个和最后一个数也要满足),如果有多组情况,必须按从小到大的顺序输出,
     
    附上代码:
     
     1 #include <iostream>
     2 using namespace std;
     3 int a[30]= {0,1},b[30],sushu[50],n;  //a表示素数环中的数字,b用来标记,sushu标记是否是素数
     4 void DFS(int s,int k)   //s代表当前循环到第几个数,k标记此时是否是第一次进人深搜函数
     5 {
     6     int i,j;
     7     if(s>n)
     8     {
     9         if(sushu[a[1]+a[n]])   //最后一个数字和第一个数字的和
    10         {
    11             for(i=1; i<=n; i++)  //全部满足,输出这组数据的全部数
    12             {
    13                 if(i>1) cout<<" ";
    14                 cout<<a[i];
    15             }
    16             cout<<endl;
    17         }
    18         else return;   //若最后一组数据不满足,则返回上一次循环
    19     }
    20     for(i=2; i<=n; i++)
    21     {
    22         if(k)
    23             for(j=1; j<30; j++)   //第一次进来,所有的数字都可以使用
    24                 b[j]=1;
    25         if(sushu[i+a[s-1]]&&b[i])  //i表示当前数字,a[s-1]表示上一个数字。两个数字之和为素数,且没有出现过i这个数字
    26         {
    27             a[s]=i;   //s位置上的数字记录为i
    28             b[i]=0;    //0表示为已使用
    29             DFS(s+1,0);  //一个满足条件,进入下一个数字的选择
    30             b[i]=1;   //若返回上一层循环,标记已使用了的数字必须还原为未使用
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     int i,j,t=1;
    37     for(i=0; i<50; i++)
    38         sushu[i]=1;
    39     sushu[0]=sushu[1]=0;
    40     for(i=2; i<=25; i++)
    41         if(sushu[i])
    42             for(j=i+i; j<50; j+=i)
    43                 sushu[j]=0;   //素数打表,减少代码运行时间,素数为1,非素数为0
    44     while(cin>>n)
    45     {
    46         cout<<"Case "<<t++<<":"<<endl;
    47         DFS(2,1);
    48         cout<<endl;
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    推荐一款国内首个开源全链路压测平台
    redis 你真的懂了吗?
    吊炸天的可视化安全框架,轻松搭建自己的认证授权平台!
    一条简单的更新语句,MySQL是如何加锁的?
    mysql 表删除一半数据,表空间会变小吗?
    调研字节码插桩技术,用于系统监控设计和实现
    这个开源工具把网页变成本地应用程序
    20160924-2——mysql常见问题集锦
    20160924-1——mysql存储引擎
    20160916-4:数据恢复
  • 原文地址:https://www.cnblogs.com/pshw/p/4776440.html
Copyright © 2011-2022 走看看