【HDU】1016 DFS——Prime Ring Problem

原创 2018年04月15日 16:14:57
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
 
这道题我是用dfs写的,是一道比较简单的dfs题目,适合像我这样的dfs入门者。

一遍AC,有些感动(捂脸哭

AC代码:

//
//  main.cpp
//  1016
//
//  Created by showlo on 2018/4/15.
//  Copyright © 2018年 showlo. All rights reserved.
//

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int a[25],vis[25];
int n,num;

int isPrime(int n){
    int flag=0;
    int i;
    for (i=2; i<n; i++) {
        if (n%i==0)
            flag=1;
    }
    if (flag==1)
        return 0;
    return 1;
}

int dfs(int k)
{
    int i,flag=0;
    if (k==n+1&&isPrime(a[n]+a[1])) {
        printf("%d",a[1]);
        for (i=2; i<=n; i++) {
            printf(" %d",a[i]);
        }
        printf("\n");
        return 0;
    }
    else {
        for (i=2; i<=n; i++) {
            flag=1;
            a[k]=i;
            if (vis[i]==1||isPrime(a[k]+a[k-1])==0) {
                flag=0;
                continue;
            }
            if (flag) {
                vis[i]=1;
                dfs(k+1);
                vis[i]=0;
            }
        }
    }
    return num;
}
int main() {
    int ans;
    num=1;
    while (scanf("%d",&n)!=EOF) {
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        printf("Case %d:\n",num);
        a[1]=1;
        dfs(2);
        num++;
        printf("\n");
    }
    return 0;
}

Windows Server 2008 R2 部署并管理文件服务器视频课程(DFS)

-
  • 1970年01月01日 08:00

杭电OJ——1016 Prime Ring Problem

Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagra...
  • lishuhuakai
  • lishuhuakai
  • 2012-09-24 22:56:25
  • 4939

hdu-1016Prime Ring Problem(素数环 dfs)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/...
  • shadowcw
  • shadowcw
  • 2016-07-13 00:25:47
  • 310

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ...
  • kenden23
  • kenden23
  • 2014-07-10 17:02:04
  • 942

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

Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram....
  • sr19930829
  • sr19930829
  • 2014-05-12 20:39:27
  • 1926

题目1016: Prime Ring Problem

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, .....
  • SJF0115
  • SJF0115
  • 2013-03-14 09:29:16
  • 3681

杭电(hdu)1016 Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • IT142546355
  • IT142546355
  • 2015-08-14 09:48:42
  • 505

杭电 HDU ACM 1016 Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • lsgqjh
  • lsgqjh
  • 2015-05-04 00:01:53
  • 671

HDU Prime Ring Problem

打开题目链接 Prime Ring Problem                                                              Time ...
  • zxdfc
  • zxdfc
  • 2014-03-30 17:17:20
  • 374

hdu_1016(剪枝深度优先搜索)

#include #include #include using namespace std; int a[6]; bool prime[40]; int n; bool vis[25]; void ...
  • qq_39557517
  • qq_39557517
  • 2017-07-24 15:34:40
  • 130
收藏助手
不良信息举报
您举报文章:【HDU】1016 DFS——Prime Ring Problem
举报原因:
原因补充:

(最多只允许输入30个字)