Prime Ring Problem(DFS)
2014-04-28 21:01
169 查看
Prime Ring Problem
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 5 Accepted Submission(s) : 4
Font: Times New Roman | Verdana |
Georgia
Font Size: ← →
Problem 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 two adjacent circles should be a prime.Note: the number of first circle should always be 1.
Input
n (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 lexicographicalorder.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6 8
Sample Output
Case 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 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2 [code]#include<iostream> #include<string.h> using namespace std; int n,f[21]; int prime[50]; int DFS(int x[21],int s[21],int l) { if(l==n) { if(prime[1+s[l]]==0) { cout<<"1"; for(int i=2;i<=n;i++) cout<<" "<<s[i]; cout<<endl; } return 0; } else { for(int j=2;j<=n;j++) if(x[j]==0&&prime[j+s[l]]==0) { s[l+1]=j,x[j]=1; DFS(x,s,l+1); if(j<=n) x[j]=0; } } return 0; } int main() { int s[21],x[21],l,sum; sum=0; memset(prime,0,sizeof(prime)); for(int i=2;i<=25;i++) for(int j=2;j*i<=50;j++) prime[j*i]=1; while(cin>>n) { sum++; memset(x,0,sizeof(x)); cout<<"Case "<<sum<<":"<<endl; s[1]=1;l=1; DFS(x,s,l); cout<<endl; } return 0; }
[/code]
相关文章推荐
- [HDU] 1016 Prime Ring Problem(DFS)
- hdu1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem(DFS入门)
- HDU1016 Prime Ring Problem DFS 简单题
- HDU1016:Prime Ring Problem(DFS)
- Vjudge Prime Ring Problem (dfs)
- Prime Ring Problem (dfs)
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
- HDU 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem (经典DFS)
- hdu1016 Prime Ring Problem(回溯dfs)
- 杭电 1016 Prime Ring Problem【DFS】
- Prime Ring Problem(DFS)
- HDU Prime Ring Problem (DFS+素数打表)
- 抛抛DFS——— Prime Ring Problem
- JD 题目1459:Prime ring problem (dfs)
- HDU 1016 Prime Ring Problem(DFS回溯+素数判断)
- HDU1016 Prime Ring Problem 解题报告--dfs
- 搜索专题(DFS)HDU 1016-Prime Ring Problem