-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA_00524.cpp
49 lines (42 loc) · 964 Bytes
/
UVA_00524.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
int N, p[33], out[17] = {1};
void backtrack(int node, int vis[]);
int main(void)
{
int n, ts = 0;
p[3] = p[5] = p[7] = p[11] = p[13] = p[17] = p[19] = p[23] = p[29] = p[31] = 1;
while(scanf("%d", &N) != EOF)
{
if(ts)
printf("\n");
printf("Case %d:\n", ++ts);
int vis[N+1] = {0};
backtrack(1, vis);
}
return 0;
}
void backtrack(int node, int vis[])
{
int i;
if(node == N)
{
if(p[out[node-1]+1])
{
printf("1");
for(i = 1; i < N; i++)
printf(" %d", out[i]);
printf("\n");
}
}
else
{
for(i = 2; i <= N; i++)
if(!vis[i] && p[i+out[node-1]])
{
vis[i] = 1;
out[node] = i;
backtrack(node + 1, vis);
vis[i] = 0;
}
}
}