• HDU 1016: Prime Ring Problem


    6

    8

    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深搜,满足条件就加入num数组,并把加入的数置零。

    因为数据范围在20以内,素数加和必定小于40,可以放个素数表~

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <map>
    #define range(i,a,b) for(int i=a;i<=b;++i)
    #define LL long long
    #define rerange(i,a,b) for(int i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    using namespace std;
    int prime[15]={2,3,5,7,11,13,17,19,23,29,31,37};
    int n,c,num[25],allnum[55];
    void init() {
        c=0;
    }
    bool isprime(int num){
        range(i,0,11)if(num==prime[i])return true;
        return false;
    }
    bool dfs(int pre,int pos,int flag){
        if(!isprime(pre+pos))return false;
        num[flag]=pos;
        if(flag==n&&isprime(pos+1)){
            range(i,1,n)cout<<num[i]<<(i==n?'
    ':' ');
            return true;
        }
        allnum[pos]=0;
        range(i,2,n)if(allnum[i]&&dfs(pos,i,flag+1))break;
        allnum[pos]=1;
        return false;
    }
    void solve(){
        while(cin>>n){
            range(i,1,n)allnum[i]=i;
            num[1]=1;
            cout<<"Case "<<++c<<":"<<endl;
            if(n==1){cout<<1<<endl;continue;}
            range(i,2,n)dfs(1,i,2);
            cout<<endl;
        }
    }
    int main() {
        init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    c#对XML读取
    WPF--TypeConverter使用
    WPF---对于没有Command属性的添加以下代码可以达到有Command效果
    自定义事件、属性、方法
    读取Excel文件
    ClickOnce安装部署,手动。
    Logger 日志记录
    Maven
    等待与通知范式
    线程状态及基本方法
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9332581.html
Copyright © 2020-2023  润新知