杭电1016 Prime Ring Problem(DFS)

来源:互联网 发布:经传软件跟风统计 编辑:程序博客网 时间:2024/06/10 20:48

Prime Ring Problem

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


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

You are 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 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
/*以前在做过,还好一次过了,每次提交都是各种纠结,加油!!!Time:2014-8-24 15:13*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;bool prime[60]={1,1,0,0};void Get_Prime(){for(int i=2;i<22;i++){if(!prime[i])for(int j=i*i;j<55;j+=i)prime[j]=1;}}int N;int store[30];bool vis[22];void DFS(int k){if(k==N){if(!prime[store[k-1]+1]){for(int i=0;i<N;i++){if(i)printf(" ");printf("%d",store[i]);}printf("\n");}return;}else{for(int i=2;i<=N;i++){if(!vis[i]&&prime[i+store[k-1]]==0){store[k]=i;vis[i]=1;DFS(k+1);vis[i]=0;}}}}void solve(){Get_Prime();int d=0;while(scanf("%d",&N)!=EOF){memset(vis,0,sizeof(vis)); if(N&1||!N)continue;printf("Case %d:\n",++d);store[0]=1;DFS(1);printf("\n");}}int main(){solve();return 0;} 
//MLE快哭了。。。。还说练习练习函数用法呢。。。。Time:2015-5-20 19:16import java.io.BufferedInputStream;import java.util.Scanner;import java.util.HashMap;public class Main {static int a[]=new int[15];static boolean []vis=new boolean [15];static HashMap<Integer,Boolean>mp= new HashMap<Integer,Boolean>();public static void main(String[] args) {// TODO Auto-generated method stubScanner cin=new Scanner(System.in);//(new BufferedInputStream(System.in));F(mp);int n,nCase=1;while(cin.hasNextInt()){n=cin.nextInt();a[0]=1;vis[1]=true;System.out.println("Case "+nCase+":");nCase++;if(n==1){System.out.println(1+"\n");continue;}if(n%2==1){continue;}else{DFS(1,n);System.out.println("");}}}private static void DFS(int k,int n){if(k==n&&mp.get(a[k-1]+1)!=null){for(int i=0;i<n-1;i++){System.out.print(a[i]+" ");}System.out.println(a[n-1]);return;}else{for(int i=2;i<=n;i++){if(a[k-1]%2==1&&i%2==1)continue;if(a[k-1]%2==0&&i%2==0)continue;if(!vis[i]&&mp.get(a[k-1]+i)!=null){//System.out.println("--"+tmp+"--");vis[i]=true;a[k]=i;DFS(k+1,n);vis[i]=false;}}}}private static void F(HashMap<Integer,Boolean>mp){mp.put(2, true); mp.put(3,true);mp.put(5, true); mp.put(7,true);mp.put(11, true); mp.put(13,true);mp.put(17, true); mp.put(19,true);mp.put(23, true); mp.put(29,true);mp.put(31, true); mp.put(37,true);}}


0 0