hdu 1016 Prime Ring Problem(dfs)

来源:互联网 发布:会编程可以在家工作吗 编辑:程序博客网 时间:2024/05/21 07:54

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

错了不低于五遍才发现输出的格式错了发火

很明显的深搜题目

#include <stdio.h>#include <string.h>int prime[12] = {2,3,5,7,11,13,17,19,23,29,31,37};//打表素数int isPrime(int p)//判断素数{    int i;    for(i = 0; i < 12;i++)        if(p == prime[i])            return 1;    return 0;}int n,book[21],record[21];void DFS(int step){    int i;    if(step == n+1 && isPrime(record[step-1]+1))    {        for(i = 1; i <= n; ++i)        {            printf("%d",record[i]);            if(i != n) printf(" ");        }        printf("\n");        return ;    }    for(i = 2; i <= n; ++i)        if(book[i] == 0 && isPrime(i+record[step-1]))        {                record[step] = i;                book[i] = 1;//把用过的标记上                DFS(step+1);                book[i] = 0;//取消标记,使重新求串时不会受到干扰        }    return ;}int main(){    int time = 0;    record[1] = 1;    while(scanf("%d",&n) != EOF)    {        ++time;        printf("Case %d:\n",time);        memset(book,0,sizeof(book));        DFS(2);//深搜        printf("\n");    }    return 0;}


0 0
原创粉丝点击