zoukankan      html  css  js  c++  java
  • HDU1016 DFS+回溯(保存路径)

    Prime Ring Problem

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


    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
     
    题意   给你一个n  对1 ... n   n个数排序 1必须在第一位 两个数相加要为素数(1与最后一个数相加也必须为素数) 输出所有满足上述的序列。
    解析 我们先1~n分别求出与它相加为素数的数存进数组,然后就相当于建了一个图 从1出发 一层一层向下推 基础DFS 然后在回溯一下 记录路径就好了
    AC代码
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <vector>
    11 using namespace std;
    12 const int maxn= 25;
    13 const int maxm= 1e4+10;
    14 const int inf = 0x3f3f3f3f;
    15 typedef long long ll;
    16 int visit[maxn],order[maxn];
    17 vector<int> v[maxn];
    18 int n,cnt;
    19 int isPrime(int num )
    20 {
    21      int tmp =sqrt( num);
    22      for(int i= 2;i <=tmp; i++)
    23         if(num %i== 0)
    24           return 0 ;
    25      return 1 ;
    26 }
    27 void dfs(int x)
    28 {
    29     visit[x]=1;    //标记访问过
    30     order[cnt++]=x; //记录路径
    31     for(int i=0;i<v[x].size();i++)    //df搜边
    32     {
    33         if(visit[v[x][i]]==0)
    34         {
    35             dfs(v[x][i]);
    36         }
    37     }
    38     visit[x]=0;    //回溯
    39     if(cnt==n&&isPrime(x+1))   //长度为n且最后一个数与1相加为素数
    40     {
    41         for(int i=0;i<cnt;i++) 
    42         {
    43             if(i==cnt-1)
    44                 printf("%d
    ",order[i]);
    45             else
    46                 printf("%d ",order[i]);
    47         }
    48     }
    49     cnt--;
    50 }
    51 int main()
    52 {
    53     int kase=1;
    54     while(scanf("%d",&n)!=EOF)
    55     {
    56         for(int i=1;i<=n;i++)
    57             v[i].clear();
    58         for(int i=1;i<=n;i++)
    59         {
    60             for(int j=1;j<=n;j++)
    61             {
    62                 if(isPrime(i+j)==1&&i!=j)    //保存能够与i相加为素数的数
    63                     v[i].push_back(j);
    64             }
    65         }
    66 //        for(int i=1;i<=n;i++)
    67 //        {
    68 //            cout<<i;
    69 //            for(int j=0;j<v[i].size();j++)
    70 //                printf(" %d",v[i][j]);
    71 //            cout<<endl;
    72 //        }
    73         memset(visit,0,sizeof(visit));   //初始化访问数组 0未访问过
    74         printf("Case %d:
    ",kase++);
    75         cnt=0;   //路径长度初始化
    76         dfs(1);  
    77         printf("
    ");
    78     }
    79 }
     
  • 相关阅读:
    文件处理
    字符编码的了解以及简单的文件处理
    python list去重加set排序
    数据的类型以及内置方法
    Python 入门 Day5
    Python 入门 Day3
    Python的入门
    计算机硬件
    【0903 | Day 29】反射和内置方法
    【0902 | Day 28】绑定方法和非绑定方法
  • 原文地址:https://www.cnblogs.com/stranger-/p/7805782.html
Copyright © 2011-2022 走看看