【HDU】1016 DFS——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.


 

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
68
 

Sample Output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 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;
}

代码(更新),做过了一下dfs的题之后,回来看基础题,发现代码中有许多没用的东西

//
//  main.cpp
//  1016.1
//
//  Created by showlo on 2018/5/17.
//  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++) {   //判断条件,有没有小于n大于1的因子
        if (n%i==0)
        {
            flag=1;
            break;
        }
    }
    if (flag==1)
        return 0;
    return 1;
}

void dfs(int k)
{
    int i;
    if (k==n+1&&isPrime(a[n]+a[1])) {   //搜索到第n+1个数,如果构成圆环,输出
        printf("%d",a[1]);
        for (i=2; i<=n; i++) {
            printf(" %d",a[i]);
        }
        printf("\n");
        return;
    }
    else {
        for (i=2; i<=n; i++) {
            a[k]=i;
            if (vis[i]==1||isPrime(a[k]+a[k-1])==0)
                continue;
            else{
                vis[i]=1;
                dfs(k+1);
                vis[i]=0;
            }
        }
    }
    return;
}
int main() {
    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;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页