A - Prime Ring Problem

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.

Inputn (0 < n < 20). 
OutputThe 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

题目分析 :

    1. 1的位置不变,从1开始DFS深搜;    2. 素数函数的效率决定了代码是否超时;

错误点 :     1. 素数的判断有误;    2. while(true) 的形式有误,无法跳出,时间超时;
#include <iostream>
#include <math.h>
#include <vector>
#include <cstring>
using namespace std;
int n,m,array[25],mark[25],step,k=0;

bool Exame_prime(int x,int y)
{
    int sum = x+y;
    for(int i=2;i*i<=sum;i++)
        if(sum%i==0)
            return true;
    return false;
}

void DFS(int x)
{
    step++;
    array[step] = x;
    if(step==n)
    {
        if(!Exame_prime(array[step],1))
        {
            for(int i=1;i<=step;i++)
            {
                if(i!=step)
                    cout << array[i] << " ";
                else
                    cout << array[i];
            }
            cout << endl;
        }
        return;
    }

    for(int i=2;i<=n;i++)
    {
        //cout <<"|||| :" <<  x <<"  " <<i << "  " <<mark[i] <<"  "  << ++k <<  "\n";
        if(!Exame_prime(x,i) && !mark[i])
        {
            mark[i]=1;
            DFS(i);
            mark[i]=0;
            step--;//不可省略,step为常数,不会回溯;
        }
    }
}

int main()
{
    int m=0;
    while(cin >> n)
    {
        memset(mark,0,sizeof(mark));
        step=0;
        cout << "Case " << ++m <<":" << "\n";
        DFS(1);
        cout << endl;
    }
    return 0;
}
时间: 2024-10-12 00:10:31

A - Prime Ring Problem的相关文章

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): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

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 of first circle should always be 1

[ACM] 1016 Prime Ring Problem (深度优先搜索)

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 of first circle

uva 524 prime ring problem——yhx

  Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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

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): 32872    Accepted Submission(s): 14544 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

HDOJ-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): 47217    Accepted Submission(s): 20859 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

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存储放置的数 /

HDU1016 Prime Ring Problem

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

【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