hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

简介: hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 74594    Accepted Submission(s): 31690


 

Problem Description

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.

 

 

Input

n (0 < n < 20).

 

 

Output

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.

 

 

Sample Input

 

6

8

 

 

Sample Output

 

Case 1:

1 4 3 2 5 6

1 6 5 2 3 4

Case 2:

1 2 3 8 5 6 7 4

1 2 5 8 3 4 7 6

1 4 7 6 5 8 3 2

1 6 7 4 3 8 5 2

 

 

Source

Asia 1996, Shanghai (Mainland China)

 

题意大致就是说要找到由1~n组成的环,且相邻的数两两相加都要是素数,输出所有满足条件的序列。

AC代码如下:

#include<stdio.h>
#include<string.h>
int f[100]={0};
int ans[21],v[21],n;
void dfs(int i)
{
    int m;
    if(i==n&&f[ans[i-1]+ans[0]]==0) //找到8个满足条件的数时输出
    {
        for(m=0;m<n-1;m++)
            printf("%d ",ans[m]);
        printf("%d\n",ans[n-1]);
    }
    else                      
    {
        for(m=2;m<=n;m++)        //找未用过的数
        {
            if(v[m]==0)          //找到还没有用过的数
            {
                if(f[m+ans[i-1]]==0)  //如果满足相邻数和为素数
                {
                    v[m]=-1;     //标记已用过
                    ans[i++]=m;  //将m放进数组
                    dfs(i);      //找下一个数
                    v[m]=0;      //接触标记
                    i--;         //回溯
                }
            }
        }
    }
}
int main()
{
    int i,j,cases=0;
    //素数打表
    for(i=2;i<100;i++)
        if(f[i]==0)
            for(j=i;i*j<100;j++)
                f[i*j]=1;
    while(scanf("%d",&n)!=EOF)
    {
        cases++;
        ans[0]=1;
        printf("Case %d:\n",cases);
        memset(v,0,sizeof(v));
        dfs(1);
        printf("\n");
    }
    return 0;
}
目录
相关文章
|
7月前
|
Java
hdu-1016-Prime Ring Problem
hdu-1016-Prime Ring Problem
33 0
|
7月前
G - Prime Ring Problem(深搜)
G - Prime Ring Problem(深搜)
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
34 0
HDU-1016,Prime Ring Problem(DFS+素数)
HDU-1016,Prime Ring Problem(DFS+素数)
|
算法
HDU-1050,Moving Tables(不用贪心也AC)
HDU-1050,Moving Tables(不用贪心也AC)
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDOJ 1016 Prime Ring Problem素数环【深搜】
121 0
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDOJ/HDU 1022 Train Problem I(模拟栈)
HDOJ/HDU 1022 Train Problem I(模拟栈)
125 0
HDOJ/HDU 1022 Train Problem I(模拟栈)
|
C语言
HDOJ 1016 Prime Ring Problem素数环【深搜2】
HDOJ 1016 Prime Ring Problem素数环【深搜】
99 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
112 0