zoukankan      html  css  js  c++  java
  • Prime Ring Problem

    Prime Ring Problem

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 13   Accepted Submission(s) : 5

    Font: Times New Roman | Verdana | Georgia

    Font Size:

    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

    Asia 1996, Shanghai (Mainland China)
    这题我用广搜 递归没有理解
    每次都从2开始 加 
     这一个组合最后一位+下一个没有被标记过的数 相加
    并判断 这两个数的和是不是素数
    如果是素数 就进队列;
    如果 这个组合的最后一位 的位置与 n相同 就要判断他与头相加 是否为素数
    #include<iostream>
    #include<algorithm>
    #include<queue>
    struct word
    {
        bool used[25];//记录该数字组合已使用过的数
        int end[25];//记录数字组合
        int ci;//记录次数
    };
    using namespace std;
    int sushu[15]={1,2,3,5,7,11,13,17,19,23,29,31,37,41,43};//素数
    //记录数据
    bool check(int a)
    {
        if(a==1)
            return 0;
        for(int i=0;i<15;i++)
        {
            if(a==sushu[i])
                return 1;//是素数
        
        }
        return 0;
    }
    int main()
    {
        int n;
        int m=0;
        while(scanf("%d",&n)!=EOF)
        {
            m++;
            printf("Case %d:
    ",m);
            queue<word> q;
            word x1;
            x1.ci=1;
            memset(x1.used,0,sizeof(x1.used));
            memset(x1.end,0,sizeof(x1.end));
            x1.end[1]=1;
            x1.used[1]=1;//开头一位是1;
            q.push(x1);
            while(!q.empty())
            {
                word now;
                now=q.front();
                q.pop();
                if(now.ci==n)
                {
                    if(check(now.end[n]+1))//到达最后一位 且 条件符合 输出
                    {
                        printf("1");
                        for(int j=2;j<=n;j++)
                        {
                            printf(" %d",now.end[j]);
                        
                        }
                        printf("
    ");
                    
                    }
                
                }
                else 
                {
                    for(int lin=2;lin<=n;lin++)
                    {
                        if(now.used[lin]==0)
                        {
                            if(check(now.end[now.ci]+lin)&&now.ci<n)//如果是素数 现在的位置 加上下一位
                            {
                                now.used[lin]=1;
                                now.ci++;
                                now.end[now.ci]=lin;
                                q.push(now);//放进队列里
                                now.ci--;
                                now.used[lin]=0;//返回原值
                            }
                            if(check(now.end[now.ci]+now.end[1])&&now.ci==n)//若为最后一位 则与头相加;
                            {
                                now.used[lin]=1;
                                now.ci++;
                                now.end[now.ci]=lin;
                                q.push(now);
                                now.used[lin]=0;
                                now.ci--;
    
                                
                            
                            }
                        }
    
    
                    }
                
                
                }
            
                
            }
            printf("
    ");
            
    
        }
                
        
            
    
        return 0;
    }
     
  • 相关阅读:
    集训Day 7 2020.3.7 动态规划(二)
    集训Day 6 2020.3.6 动态规划(一)
    集训Day 5 2020.3.4 杂题选讲(二)
    集训Day 4 2020.3.3 杂题选讲(一)
    集训Day 2 2020.3.1 数论(质数与筛法)
    集训Day 1 2020.2.29 数论复习(gcd)(一)
    [BZOJ4152]The Captain
    知识点清单(全)
    字符串相关知识
    分块相关知识
  • 原文地址:https://www.cnblogs.com/2013lzm/p/3265794.html
Copyright © 2011-2022 走看看