-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_ring_problem.cpp
44 lines (40 loc) · 999 Bytes
/
prime_ring_problem.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
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int P[60] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 };
int a[23], v[23] = {0}, N;
void handle(int n);
int main()
{
int cnt = 0;
while((scanf("%d", &N)) != EOF){
memset(v, 0, sizeof(v));
cnt++;
for(int i = 0; i < N; i++){
a[i] = i + 1;
}
printf("Case %d:\n", cnt);
handle(1);
printf("\n");
}
return 0;
}
void handle(int n){
if(n == N && P[a[0] + a[n - 1]]){
printf("%d", a[0]);
for(int i = 1; i < N; i++){
printf(" %d", a[i]);
}
printf("\n");
}else{
for(int i = 2; i <= N; i++){
if(v[i] == 0 && P[i + a[n - 1]]){
a[n] = i;
v[i] = 1;
handle(n + 1);
v[i] = 0;
}
}
}
}