hdu1016 Prime ring Problem DFS prime number playing table

Source: Internet
Author: User

The meaning is to give you a number n, to form a prime ring, which is composed of 1-n, which is characterized by the selection of any number of rings on the ring i,i and it is connected to the two number added together are the prime, satisfied on the output.

The practice of this problem is almost always using DFS backtracking with the hdu1015 approach. The difference is that this is all about searching, and hdu1015 just needs to search for the first group.
Secondly, the use of prime numbers in this topic to simplify the determination of prime numbers, in some cases, the efficiency has been improved.

Prime Ring problem
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34615 Accepted Submission (s): 15331

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, and adjacent circles should is a prim E.

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 is 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

#include <cstdio>#include <cmath>#include <cstring>#define MAXNusing namespace STD;BOOLDP[MAXN][MAXN];//Memory Prime arrayintNBOOLisvisited[maxn+3];intm[maxn+3];voidIs_prime () { for(intI=1;i< -; i++) for(intj=1;j< -; j + +) {intTest=i+j;intflag=0; for(intx=2; x*x<=test;x++) {if(test%x==0) {dp[i][j]=false; flag=1; Break; }            }if(!flag) dp[i][j]=true; }}voidPT () {intfirst=0; for(intI=1; i<=n;i++) {if(first)printf(" ");Elsefirst=1;printf("%d", M[i]); }printf("\ n");}voidDfsintK) {//Select the last one.    //must not forget to decide whether to add up to the first 1 is the prime number    if(k==n&&dp[m[k]][1]){//Perform print tasksPT ();return; } for(intI=2; i<=n;i++)//Judge if this number has been used        if(isvisited[i]==false){if(Dp[i][m[k]]) {//Check the table, determine whether the addition and the prime numberisvisited[i]=true;//Mark usage statusm[k+1]=i;//Current value is already desirableDFS (k +1);//Search downisvisited[i]=false;//Backtracking}        }}intMain () {intCa=1;memset(DP,false,sizeof(DP)); Is_prime ();//Play Table    while(scanf("%d", &n)!=eof) {printf("Case%d:\n", CA);memset(Isvisited,false,sizeof(isvisited)); m[1]=1;//Topic requirements The first one must be 1Dfs1);//So we've got one first, and then we're looking down.ca++;printf("\ n");//Note there is a blank line after the output is finished}return 0;} Today is good, the first one on AC, the equivalent of LOL take the first win. Ha ha.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1016 Prime ring Problem DFS prime number playing table

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.