(Hdu step 4.3.2) Prime Ring Problem (n number is a Ring, and all the conditions for the sum of the two is output), hdu4.3.2

Source: Internet
Author: User

(Hdu step 4.3.2) Prime Ring Problem (n number is a Ring, and all the conditions for the sum of the two is output), hdu4.3.2

Question:

Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 467 Accepted Submission (s): 297
 
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 ).
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 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
 
 
SourceAsia 1996, Shanghai (Mainland China)
RecommendJGShining


Question Analysis:

Simple question. Use DFS. Note that the nth number also needs to determine whether the sum of sum 1 is a prime number. It should be noted that, compared with the previous question, this question is a one-dimensional DFS question, while the last question is a two-dimensional DFS question. Therefore, this question is not applied to map [] [] of the Bao Cun map information.


DFS rules are as follows:

1) access an adjacent unaccessed node and import it to the stack

2) If rule 1 cannot be executed, an element of the stack is output.

3) If rule 1 cannot be executed, 2) the traversal is completed.


The Code is as follows:

/** B. cpp ** Created on: February 24, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <algorithm> # include <cmath> using namespace std; const int maxn = 21; int value [maxn]; // used to store the final conforming result queue bool visited [maxn]; // indicates whether a certain number has accessed int n./*** determines whether a certain number n is a prime number */bool isPrime (int n) {int I; for (I = 2; I <= sqrt (n) + 0.5; ++ I) {// If a number is a combination, name must have a factor that is <sqrt (n) if (n % I = 0) {// if it can remove ireturn fal Se; // It indicates that it is not a prime number} return true; // It indicates that it is a prime number}/*** output result queue meeting the requirements */void printValues () {int I; for (I = 1; I <n; ++ I) {printf ("% d", value [I]);} printf ("% d \ n ", value [n]);}/*** Deep Search * cnt: number of currently searched steps */void dfs (int cnt) {// if n steps have been completed, and the sum of the number of cut n and 1st is the prime number if (cnt = (n + 1) & isPrime (value [cnt-1] + 1) {printValues (); // name: this is the case that meets the requirements. Output all results} int I; for (I = 2; I <= n; ++ I) {// traverse each number, check whether it is suitable to be added to the current result queue. // If I has not been accessed & the last number and I of the current result Sequence And is a prime number if (visited [I] = false) & isPrime (I + value [cnt-1]) {visited [I] = true; // mark I as value [cnt] = I; // Add I to the result sequence dfs (cnt + 1 ); // search for the next suitable number visited [I] = false along the sequence; // the above case has been taken into consideration and I is reset to inaccessible. used to consider the next situation }}int main () {int counter = 1; while (scanf ("% d", & n )! = EOF) {memset (visited, false, sizeof (visited); printf ("Case % d: \ n", counter); value [1] = 1; // The 1st Number of sequences is always 1 visited [1] = true; dfs (2); // search printf ("\ n") from step 1 "); counter ++;} return 0 ;}




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.