Hdu 1016 Prime Ring Problem (deep first search), hdu deep first search

Source: Internet
Author: User

Hdu 1016 Prime Ring Problem (deep first search), hdu deep first search
Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 12105 Accepted Submission (s): 5497

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 Input6 8

 

Sample OutputCase 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 6 1 7 6 5 8 3 2 1 6 7 4 3 8 5 2 input n to find 1 ~ The combination of n, so that the adjacent two numbers are the prime number. Analysis: preprocessing the prime number between 40, and then backtracking; 1 # include <iostream> 2 # include <cstring> 3 # define N 25 4 # define M 40 5 using namespace std; 6 7 bool is_prime [M], visited [N]; 8 int n, test, ans [N]; 9 10 void work (int k) 11 {12 int I; 13 if (k = n + 1) 14 {15 if (! Is_prime [ans [n] + ans [1]) return; 16 for (I = 1; I <= n-1; I ++) 17 cout <ans [I] <"; 18 cout <ans [I] <endl; 19 return; 20} 21 for (I = 2; I <= n; I ++) 22 {23 if (! Visited [I] & is_prime [ans [k-1] + I]) 24 {25 visited [I] = true; 26 ans [k] = I; 27 work (k + 1); 28 visited [I] = false; 29} 30} 31} 32 33 bool prime (int n) 34 {35 if (n = 1) return false; 36 if (n = 2 | n = 3) return true; 37 int I; 38 for (I = 2; I <n; I ++) 39 if (n % I = 0) 40 return false; 41 return true; 42} 43 44 int main () 45 {46 int I; test = 1; 47 for (I = 1; I <M; I ++) is_prime [I] = prime (I); 48 while (cin> n) 49 {50 ans [1] = 1; 51 memset (visited, false, sizeof (visited); 52 cout <"Case" <test <": "<endl; 53 work (2); 54 test ++; 55 cout <endl; 56} 57 return 0; 58}View Code

 

Related Article

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.