#include <stdio.h>
#include <stdlib.h>

int n;
static int visited[20];
static int stack[20];
int top;


int prime[38] =
{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};

void dfs (int i,int top)



{
stack[top] = i;
if (top == n - 1)

{
for (int m = 2; m <= n; m ++)

{
if (prime[stack[top] + m] && prime[m + 1] && !visited[m])

{
stack[top + 1] = m;
//printf ("%d\n",m);
for (m = 1; m <= n; m ++)

{
printf (m == 1 ? "%d" :" %d", stack[m]);
}
printf ("\n");
}
}
}


else


{

for (int j = 1; j <= n; j ++)


{

if (prime[i + j] && !visited[j])


{
visited[j] = 1;
dfs (j,top+1);
visited[j] = 0;

}

}

}

}


int main ()


{
int cn = 0;
while ( scanf ("%d", &n) != EOF )

{
printf ("Case %d:\n",++ cn);
visited[1] = 1; top = 1;
dfs (1,top);
printf ("\n");
}
return 0;
}

posted on 2010-08-18 14:25
雪黛依梦 阅读(418)
评论(0) 编辑 收藏 引用 所属分类:
搜索---DFS BFS