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 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

代码:

#include <iostream>
#include <cstring>
using namespace std;
bool 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 };
bool vis[22];
int n, ring[22];
void ans() {
    cout << ring[0];
    for (int i = 1; i < n; i++) {
        cout << ‘ ‘ << ring[i];
    }
    cout << ‘\n‘;
}
void dfs(int pos) {
    if (pos == n &&prime[ring[n - 1] + ring[0]]) {
        ans();
        return;
    }
    for (int i = 2; i <= n; i++) {
        if (!vis[i] && prime[i + ring[pos - 1]]) {
            vis[i] = true;
            ring[pos] = i;
            dfs(pos + 1);
            vis[i] = false;
        }
    }
}
int main()
{
    int case_num = 1;
    ring[0] = 1;
    while (cin >> n) {
        memset(vis, false, sizeof(vis));
        cout << "Case " << case_num << ":" << endl;
        case_num++;
        dfs(1);
        cout << endl;
    }
    return 0;
}
时间: 2024-10-05 05:12:07

HDOJ-1016 Prime Ring Problem【DFS】的相关文章

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

hdoj - 1258 Sum It Up &amp;&amp; hdoj - 1016 Prime Ring Problem (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #include <cstring> 3 4 int n,t; 5 int b[15],c[15]; 6 bool flag; 7 void dfs(int k,int sum,int l) 8 { 9 if(sum==t) 10 { 11 for(int i=0;i<l-1;i++) 12 p

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

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

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

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

hdoj 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

HDOJ 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

UVa 524 Prime Ring Problem【回溯】

题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<m