您的位置:首页 > 其它

【暴力搜索】[HDU 1016]Prime Ring Problem

2015-07-24 16:35 330 查看
这道题太简单不多赘述。打个表,随便搞。。

[code]#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int prime[40] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0};
bool vis[21];
int n, t[21];
void dfs(int u){
    if(u > n){
        if(!prime[t
 + 1]) return ;
        printf("%d", t[1]);
        for(int i=2;i<=n;i++)
            printf(" %d", t[i]);
        puts("");
        return ;
    }
    for(int i=2;i<=n;i++)if(!vis[i] && prime[i + t[u-1]]){
        t[u] = i;
        vis[i] = true;
        dfs(u+1);
        vis[i] = false;
    }
}
int main(){
    t[1] = 1;int tcnt=0;
    while(~scanf("%d", &n)){
        printf("Case %d:\n", ++tcnt);
        dfs(2);
        puts("");
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: