(Hdu step 4.3.2) Prime ring problem (n number of rings, output 22 and all cases of prime numbers)

Source: Internet
Author: User

Topic:

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

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 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
Sourceasia 1996, Shanghai (Mainland China)
Recommendjgshining


Topic Analysis:

Simple question. Use DFS to do it. It is important to note that the nth number also needs to be judged and 1 and whether it is a prime. It is important to note that this is a one-dimensional DFS topic compared to the previous question, and the last is a two-dimensional DFS topic. So this question does not open the map[][of the map information of the village.


The rules for DFS are as follows:

1) Access an adjacent unreachable node and put it into the stack.

2) If you cannot execute Rule 1), then stack an element

3) If you cannot perform rule 1) 2), the traversal is completed


The code is as follows:

/* * b.cpp * * Created on:2015 February 24 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <algorithm> #include <cmath>using namespace std;const int maxn = 21;int value[maxn];//  Used to store the final conforming result queue bool visited[maxn];//is used to mark whether a number has accessed an int n;/** * To determine whether a number n is prime */bool isprime (int n) {int i;for (i = 2; I <= SQRT (n) + 0.5; ++i) {//If a number is composite, name it must have a factor of &LT;SQRT (n) if (n%i = = 0) {//If it can divide Ireturn false;//description He is not prime number}}return true;//description He is prime number}/** * The output conforms to the required result queue */void printvalues () {int i;for (i = 1; i < n; ++i) {printf ("%d", Value[i]);} printf ("%d\n", Value[n]);} /** * CNT: The number of steps currently searched */void dfs (int cnt) {//If you have already walked n steps and Chedi N and 1th number are prime if (cnt = = (n+1) && IsPrime (Value[cnt-1] + 1 ) {printvalues ();//name this condition is in compliance with the condition, output all results}int i;for (i = 2; I <= n; ++i) {//traverse each number to see if he is suitable to join in the result queue for this situation now//If I have not visited && the last number of the current result sequence with I and is a prime if ((visited[i] = = False) && IsPrime (I+value[cnt-1])) {Visited[i] = true;// Mark I as having visited value[cnt] = i;//add I to DFS (cnt+1) in the result sequence;//along theSequence to find the next suitable number visited[i] = false;//above a situation has been considered, and I was reset to not visited. Used to consider the next case}}}int main () {int counter = 1;while (scanf ("%d",& N)!=eof) {memset (visited,false,sizeof (visited));p rintf ("Case%d:\n", counter); value[1] = the 1th number of 1;//sequences is always 1visited[1] = True;dfs (2);//Start searching for printf ("\ n") from the 2nd step; counter++;} return 0;}




(Hdu step 4.3.2) Prime ring problem (n number of rings, output 22 and all cases of prime numbers)

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.