-
Title 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 the adjacent circles should is a PR Ime.
Note:the number of first circle should always be 1.
-
Input:
-
N (1 < n < 17).
-
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:
-
68
-
-
Sample output:
-
Case 1:1 4 3 2 5 a 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
Code:
#include <cstdio>using namespacestd;Const intArrsize= A;intAns[arrsize];//Save the number of each placed in the ringBOOLHash[arrsize];//the number of tokens that have been placed in the ring beforeintN;intprime[]={2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, A};//A set of primes within a given range BOOLIsPrime (intx) { for(intCnt=0;cnt< -;++CNT) { if(x==prime[cnt])return true; } return false;} voidCheck () {//Check the solution that the output is enumerated by the backtracking method if(IsPrime (ans[n]+ans[1])==false)//determines whether the last number and the first number are prime and if not, returns directly return; for(intCnt=1; cnt<=n;++CNT) { if(cnt!=1) printf (" "); printf ("%d", ans[cnt]); } printf ("\ n");} voidDasointNUM) {//recursive enumeration, NUM is the number of digits that have been placed in the ANS array if(num>1){//when the number is placed greater than 1 o'clock if(IsPrime (ans[num]+ans[num-1])==false) return; } if(num==n) {//When the number of placed numbers equals ncheck (); return; } for(intCnt=2; cnt<=n;++CNT) { if(hash[cnt]==false) {hash[cnt]=true; Ans[num+1]=CNT; DFS (Num+1); HASH[CNT]=false;//if the current number of num+1 is determined to be ans[1] to ans[num+1],}//all cases have been processed, so hash[i] should be re-marked as unused. }} intMain () {intcas=0; while(SCANF ("%d", &n)! =EOF) { ++CAs; for(intCnt=0; cnt<arrsize;++CNT) {hash[cnt]=false; } ans[1]=1; printf ("Case %d:\n", CAs); hash[1]=true; DFS (1); printf ("\ n"); } return 0;} /************************************************************** problem:1459 User:lcyvino language:c++ Re sult:accepted time:590 Ms memory:1020 kb****************************************************************/
Topic 1459:prime Ring Problem