zoukankan      html  css  js  c++  java
  • hdu1016 prime ring problem

             

    Problem : 1016 ( Prime Ring Problem ) Judge Status : Accepted
    RunId :
    3644950 Language : C++ Author : zjut11018
    回溯法(同八皇后问题)DFS
    #include
    <stdio.h>
    #include
    <algorithm>
    usingnamespace std;
    int vis[41],pri[21],c[21],n;
    void dfs(int cur)
    {
    if(cur==n)
    {
    if(!pri[c[cur-1]+1])//注意判断首尾相加是否是素数
    {
    for(int i=0;i<n-1;i++)
    printf(
    "%d ",c[i]);
    printf(
    "%d\n",c[n-1]);
    }
    //此处不可以写return;否则就回到主函数结束dfs
    }
    else
    {
    for(int j=2;j<=n;j++)
    {

    if(!vis[j]&&!pri[c[cur-1]+j])//判断此数没用过&&是素数
    {
    vis[j]
    =1;//标记
    c[cur]=j;//c[]用于打印
    dfs(cur+1);
    vis[j]
    =0;//清楚标记 重要!!
    }
    }

    }
    }
    int main()
    {
    for(int i=2;i<=40;i++)
    for(int j=i*2;j<=40;j+=i)pri[j]=1;//pri[]=1表示素数
    int cas=0;
    while(scanf("%d",&n)==1)
    {
    cas
    ++;
    printf(
    "Case %d:\n",cas);
    memset(vis,
    0,sizeof(vis));
    memset(c,
    0,sizeof(c));
    c[
    0]=1;//注意第一个数
    dfs(1);
    printf(
    "\n");
    }
  • 相关阅读:
    向IPython Notebook中导入.py文件
    python--时间日期
    python--条件和循环
    python--输入输出
    python--字符串
    python--内置函数
    python--异常
    python--模块
    python--数据结构
    pybrain
  • 原文地址:https://www.cnblogs.com/sook/p/1988328.html
Copyright © 2011-2022 走看看