Hdu1016 prime ring problem (deep Priority Search)

Source: Internet
Author: User

Prime ring Problem Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 27488 accepted submission (s): 12248

Problem descriptiona ring is compose of N circles as shown in digoal. put Natural Number 1, 2 ,..., N into each circle separately, and the sum of numbers in two adjacent circles shoshould be a prime.

Note: The number of First Circle shoshould always be 1.



Inputn (0 <n <20 ).

Outputthe 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 abve process.

Print a blank line after each case.

Sample Input
68

Sample output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
Problem description: round the N integers from 1 to n into a ring. If any two adjacent numbers are added and the result is a prime number, the ring becomes a prime number ring. When n = 20, the following sequence is a prime ring: 1 2 3 4 7 6 5 8 9 10 13 16 15 17 20 11 12 19 18
The idea is as follows: filter all the conditions within 20 except the odd number. If the adjacent values can be added as prime numbers at the same time, and ensure that the first use in the array is optional, continue to search downward. For the determination of prime numbers, we recommend that you create tables in advance to save time...
#include<iostream>#include<cmath>using namespace std;int t[21];int pre[20];int N,count;int prime(int m){int x=(int)sqrt((double)m);for(int k=2;k<=x;k++){if(m%k==0)return 0;}return 1;}int judge(int m){for(int i=1;i<=N&&t[i]!=0;i++){if(m==t[i])return 0;}return 1;}int prim(int m){for(int i=0;i<20;i++)if(m==pre[i])return 1;return 0;}void DFS(int n){if(n==N+1){if(prime(t[N]+t[1])){for(int k=1;k<N;k++)cout<<t[k]<<" ";cout<<t[N]<<endl;}}for(int i=2;i<=N;i++){if(judge(i)&&prim(t[n-1]+i)){t[n]=i;DFS(n+1);t[n]=0;}}}int main(){int k=0;for(int i=2;i<40;i++)if(prime(i))pre[k++]=i;while(cin>>N&&N%2==0){count++;cout<<"Case "<<count<<":"<<endl;memset(t,0,sizeof(t));t[1]=1;DFS(2);cout<<endl;}return 0;}

Later I saw someone else and found that it was also good. There were great Optimizations in both time and space.
#include"stdio.h"#include"string.h"int n;int a[123],used[123];int ok(int n){    int i;    for(i=2;i<n;i++)    {        if(n%i==0) return 0;    }    return 1;}void dfs(int x){    int i;    if(x==n)    {        int j;        if(ok(1+a[x-1])==1)  //头尾和判断        {            printf("1");            for(j=1;j<n;j++)  printf(" %d",a[j]);  //构造够n个了 输出数组。            printf("\n");            return ;        }    }    for(i=2;i<=n;i++)    {        if(used[i]==0&&ok(i+a[x-1])==1)  //加上判断和是不是素数        {            a[x]=i;            used[i]=1;  //标记使用了            dfs(x+1);    //对第x+1个进行构造            used[i]=0;  //标记复原        }    }    return ;}int main(){    int cas=1;    while(scanf("%d",&n)!=-1)    {        memset(used,0,sizeof(used)); // 赋值都没被使用过。        used[1]=1;        a[0]=1;        printf("Case %d:\n",cas++);        dfs(1);  //从第1个数开始构造,因为以1开始        printf("\n");    }    return 0;}
Note that the used array is used to mark the selected I value, while the array is the value selected from 0, this also solves the problem that an array cannot be searched by TAG.

hdu1016 prime ring problem (deep Priority Search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.