• hdu 1016 Prime Ring Problem


    http://acm.hdu.edu.cn/showproblem.php?pid=1016

    Prime Ring Problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 19551    Accepted Submission(s): 8742


    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
     
    Recommend
    JGShining
     就一个dfs
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 int num[20],vis[20],n;
     5 int is_prime(int x)
     6 {
     7      int i;
     8      for(i=2;i<=sqrt(x);i++)
     9      if(x%i==0)
    10      return 0;
    11      return 1;
    12 
    13 }
    14 void dfs(int x,int k)
    15 {
    16      int i;
    17      if(k==n &&is_prime(1+x))
    18      {
    19           for(i=0;i<n;i++)
    20           printf("%d%c",num[i],i==k-1?'
    ':' ');
    21      }
    22      for(i=2;i<=n;i++)
    23      {
    24           if(is_prime(x+i)&&!vis[i])
    25           {
    26             // printf("x=%d,i=%d,k=%d
    ",x,i,k);
    27                vis[i]=1;
    28                num[k]=i;
    29                dfs(i,k+1);
    30                vis[i]=0;
    31           }
    32      }
    33 }
    34 int main()
    35 {
    36     int count=1;
    37      while(scanf("%d",&n)!=EOF)
    38      {
    39           memset(vis,0,sizeof(vis));
    40           printf("Case %d:
    ",count++);
    41           num[0]=1;
    42           dfs(1,1);
    43           printf("
    ");
    44 
    45      }
    46      return 0;
    47 }
    View Code
     
  • 相关阅读:
    spring整合curator实现分布式锁
    curator操作zookeeper
    zk创建集群
    zookeeper下的基本操作
    java语音转文字
    netty的数据通信之心跳检测
    arm B和BL指令浅析
    NAND FLASH驱动程序
    外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
    内存接口原理图笔记
  • 原文地址:https://www.cnblogs.com/llei1573/p/3201330.html
Copyright © 2020-2023  润新知