Hdoj 1016 HDU 1016 prime ring problem ACM 1016 in HDU

Source: Internet
Author: User
Question address: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1016 Description:
Prime ring Problem

Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6150 accepted submission (s): 2745

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 Input

 
68

Sample output

Case 1:1 4 3 2 5 61 6 5 2 3 4 case 2:1 2 3 8 5 6 7 41 2 5 3 4 7 61 4 6 5 8 3 21 6 7 4 3 8 5 2 Question Analysis: Typical DFS questions, no pruning required, direct The question output is Bt. Generally, the carriage return is returned, and the first vehicle return is if (n = 1; after several PE operationsProgramLastOutput two carriage returns to a, ym ...........  Code As follows:/* miyu original, Please Note: Reproduced from __________ White House http://www.cppblog.com/MiYuAuthor by: miyutest: Program: */# include <iostream> using namespace STD; bool prim [25]; int res [25]; bool hash [25]; int N; void setprim () {memset (Prim, 0, sizeof (prim )); prim [2] = prim [3] = prim [5] = prim [7] = prim [11] = prim [13] = prim [17] = prim [19] = prim [23] = true ;} bool DFS (INT num, int N) {res [N] = num; If (n> N) {Return false;} If (n = n-1) {for (INT I = 2; I <= N; ++ I) {If (prim [num + I] & prim [I + 1] &! Hash [I]) {res [n + 1] = I; for (INT I = 1; I <= N; ++ I) printf (I = 1? "% D": "% d", Res [I]); putchar ('\ n') ;}}for (INT I = 2; I <= N; ++ I) {If (prim [num + I] &! Hash [I]) {hash [I] = true; DFS (I, n + 1); hash [I] = false ;}} return false ;} int main () {setprim (); int CA = 1; while (CIN> N) {sizeof (hash, 0, sizeof (hash); printf ("case % d: \ n ", CA ++); hash [1] = true; DFS (1, 1); putchar ('\ n');} 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.