UVa-524 Prime Ring Problem

 1 #include <bits/stdc++.h>
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 using namespace std;
 4 
 5 int isp[39] = {0,0,1,1,0,
 6                1,0,1,0,0,
 7                0,1,0,1,0,
 8                0,0,1,0,1,
 9                0,0,0,1,0,
10                0,0,0,0,1,
11                0,1,0,0,0};
12 int n;
13 int A[39];
14 int vis[39];
15 void dfs(int cur)
16 {
17     if(cur == n && isp[A[0]+A[n-1]])
18     {
19         _for(i,0,n-1)
20             printf("%d ",A[i]);
21         printf("%d\n",A[n-1]);
22     }
23     else
24         _for(i,2,n+1)
25         {
26             if(!vis[i] && isp[i+A[cur-1]])
27             {
28                 A[cur] = i;
29                 vis[i] = 1;
30                 dfs(cur+1);
31                 vis[i] = 0;
32             }
33         }
34 }
35 int main()
36 {
37      int kase = 0;
38      while(cin >> n)
39      {
40         memset(A,0,sizeof(A));
41         memset(vis,0,sizeof(vis));
42         if(kase)
43             printf("\n");
44         printf("Case %d:\n",++ kase);
45         A[0] = 1;
46         vis[1] = 1;
47         dfs(1);
48         
49     }
50     return 0;
51 }

 

posted @ 2018-11-29 20:15  Asurudo  阅读(125)  评论(0编辑  收藏  举报