【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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章