zoukankan      html  css  js  c++  java
  • [ACM] 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): 23268    Accepted Submission(s): 10363


    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
     


     

    Source

    解题思路:

    本质是dfs生成全排列,只不过本题有约束条件。由上学期期末考试时2个多小时做不出来这道题,到现在很快做出这道题,看来自己真进步了不少。继续加油!

    代码:

    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    int num[22];
    int visit[22];
    int n;
    
    bool prime(int n)
    {
        if(n==1)
            return 0;
        for(int i=2;i<n;i++)
            if(n%i==0)
        {
            return 0;
        }
        return 1;
    }
    
    void dfs(int cur)
    {
        if(cur>n&&prime(num[1]+num[n]))//输出条件
        {
            for(int i=1;i<=n-1;i++)
                cout<<num[i]<<" ";cout<<num[n];//注意格式,最后一个数后面没有空格
            cout<<endl;
        }
        else
        {
            for(int i=2;i<=n;i++)
            {
                if(prime(i+num[cur-1])&&!visit[i])//注意是num[cur-1]而不是num[i-1],因为要比较相邻的两个数,num[cur]前面的数是num[cur-1]
                {
                    visit[i]=1;
                    num[cur]=i;
                    dfs(cur+1);
                    visit[i]=0;
                }
            }
        }
    }
    
    int main()
    {
        int c=1;
        while(cin>>n)
        {
            num[1]=1;
            visit[1]=1;
            memset(visit,0,sizeof(visit));
            cout<<"Case "<<c++<<":"<<endl;
            dfs(2);
            cout<<endl;
        }
        return 0;
    }
    


    运行:

  • 相关阅读:
    pytest实际编码中特殊问题的解决
    python+locust性能测试实例
    python使用eval动态调用函数及其在测试用例中断言的应用
    python中列表生成式的两种用法
    pycharm中的Terminal无法使用git命令
    ruamel.yaml的使用
    pip安装模块失败如何解决
    locust的setup等相关函数的使用
    python模块-optparse(解析命令行参数)
    (转)locust源码目录结构及模块作用
  • 原文地址:https://www.cnblogs.com/vivider/p/3697692.html
Copyright © 2011-2022 走看看