HDU 1016:Prime Ring Problem

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25156    Accepted Submission(s): 11234

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
</pre><pre name="code" class="cpp">#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;

int date[25]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int ans[25];
int n, cnt;
int flag;

void dfs(int step)
{
    int temp;
    if(step==n)       //深搜到底时注意判断首尾是否符合要求
    {
        flag=1;
        temp = ans[0]+ans[n-1];
        if(flag)
        {
            for(int i=2; i*i<=temp; i++)
                if(temp%i==0)
                {
                    flag=0;
                    break;
                }
        }
        if(flag)
        {
            printf("%d", ans[0]);
            for(int i=1; i<n; i++)
                printf(" %d", ans[i]);
            printf("\n");
        }
    }
    else
    {
        for(int i=1; i<n; i++)
        {
            if(date[i])
            {
                temp=ans[cnt-1]+date[i];
                flag=1;
                for(int j=2; j*j<=temp; j++)
                {
                    if(temp%j==0)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                {
                    ans[cnt++]=date[i];
                    date[i]=0;
                    dfs(step+1);
                    date[i]=ans[--cnt];   //记得将数据的还原处理
                    ans[cnt]=0;
                }
            }
        }
    }
}

int main()
{
    int count=1;
    while(scanf("%d", &n)!=EOF)
    {
        memset(ans, 0, sizeof(ans));
        cnt=1;
        ans[0]=1;
        printf("Case %d:\n",count++);
        dfs(1);            //从1开始进入搜索
        printf("\n");      //每次输出后记得加个换行
    }

    return 0;
}

HDU 1016:Prime Ring Problem

时间: 2024-10-07 05:15:04

HDU 1016:Prime Ring Problem的相关文章

HDU 搜索练习 Prime Ring Problem

Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 10 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..

题目1459:Prime ring problem(素数环问题——递归算法)

题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1459 Prime ring problem.cpp // Jobdu // // Created by PengFei_Zheng on 23/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #

题目1459:Prime ring problem

时间限制:2 秒 内存限制:128 兆 特殊判题:否 提交:1888 解决:794 题目描述: 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 firs

(hdu step 4.3.2)Prime Ring Problem(n个数成环,输出两两之和为质数的所有情况)

题目: Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 467 Accepted Submission(s): 297   Problem Description A ring is compose of n circles as shown in diagram. Put natural number

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem 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

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

【dfs】hdu 1016 Prime Ring Problem

[dfs]hdu 1016 Prime Ring Problem 题目链接 刚开始接触搜索,先来一道基本题目练练手. 注意对树的深度进行dfs dfs过程中注意回退!!! 素数提前打表判断快一些 参考代码 /*Author:Hacker_vision*/ #include<bits/stdc++.h> #define clr(k,v) memset(k,v,sizeof(k)) using namespace std; const int _max=1e3+10;//素数打表 int n,pr

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb