posts - 99,  comments - 8,  trackbacks - 0
#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 雪黛依梦 阅读(402) 评论(0)  编辑 收藏 引用 所属分类: 搜索---DFS BFS

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(4)

随笔分类

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜