素数环 回溯法实现

素数环就是1-n个数,按某一序列排列,使得任意相邻2个数的和为素数。

比如1-6。素数环的排列顺序如下:

1 4 3 2 5 6
1 6 5 2 3 4

那么实现的算法,同样可以使用回溯法。

回溯法的关键思想是要找到回溯的入口点。即确定什么时候继续运算下去,什么时候停止继续,尝试下一个值。

比如对于1-6这个序列,本算法的伪代码

先把环初始化,即有0,0,0,0,0,0这样一个序列。

for i从1到6

把i放入第一个位子

检测这个环是不是素数环(忽略0,忽略首尾相加)

如果是

        继续取下一个数放到下一个位置

       直到这个环上的数字都放置了,打印此环。

如果不是

    这个位子的数字清零

如果所有的数字都尝试了,都不行

那么这个序列无法形成素数环。

其中,把这个位子清零就是回溯,代表尝试下一个值。

比如,当递归进去1234,5填入后,发现不行,则情况,再填6,也不行,此时6清零。递归退出。即set(index + 1)语句运行结束,然后再运行清零。此时清零的是4。

本质上,可以把递归调用,看成是普通调用,只是这个调用的方法,和当前方法长的一模一样而已。

具体代码如下: 

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace ConsoleApplication3  
  7. {  
  8.     public class Program  
  9.     {  
  10.  
  11.         public static List<int> lst = new List<int>();  
  12.         public static List<int> numlst = new List<int>();  
  13.         static public readonly int count = 6;  
  14.         static void Main(string[] args)  
  15.         {  
  16.             for (int i = 1; i <= count; i++)  
  17.             {  
  18.                 numlst.Add(i);  
  19.                 lst.Add(0);  
  20.             }  
  21.  
  22.             set(0);  
  23.         }  
  24.  
  25.         static public void set(int index)  
  26.         {  
  27.             //Console.WriteLine("进入递归" + index);  
  28.             #region for  
  29.             foreach (var item in numlst)  
  30.             {  
  31.                 //这句很重要,表示已经在环中的数字自动忽略掉。  
  32.                 if (lst.Contains(item))  
  33.                     continue;  
  34.  
  35.                 lst[index] = item;  
  36.                 //lst.ForEach(x => { if (x > 0) { Console.Write(x + " "); } });  
  37.                 //Console.WriteLine();  
  38.                 if (valid(lst))  
  39.                 {  
  40.                     if (index == count - 1)  
  41.                     {  
  42.                         Console.Write("OK! ");  
  43.                         lst.ForEach(x => { Console.Write(x + " "); });  
  44.                         Console.WriteLine();  
  45.                         //return;//此句表示只找一组  
  46.                     }  
  47.                       
  48.                     set(index + 1);  
  49.                 }  
  50.                 lst[index] = 0;  
  51.                 //lst.ForEach(x => { if (x > 0) { Console.Write(x + " "); } });  
  52.                 //Console.WriteLine();  
  53.             }  
  54.             #endregion  
  55.             
  56.  
  57.             //Console.WriteLine("退出递归" + index);  
  58.         }  
  59.  
  60.         static public bool valid(List<int> lst)  
  61.         {  
  62.             List<int> tmplst = lst.Where(x => x > 0).ToList();  
  63.             if (tmplst.Count < 2)  
  64.                 return true;  
  65.             if (tmplst.Count == count)  
  66.             {  
  67.                 if (!isSushu(tmplst[0] + tmplst[tmplst.Count - 1]))  
  68.                 {  
  69.                     return false;  
  70.                 }  
  71.             }  
  72.             for (int i = 0; i < tmplst.Count - 1; i++)  
  73.             {  
  74.                 if (!isSushu(tmplst[i] + tmplst[i + 1]))  
  75.                 {  
  76.                     return false;  
  77.                 }  
  78.             }  
  79.             return true;  
  80.         }  
  81.  
  82.         static public bool isSushu(int s)  
  83.         {  
  84.             if (s == 1)  
  85.                 return false;  
  86.             if (s == 2)  
  87.                 return true;  
  88.             for (int i = 2; i <= Math.Sqrt(s) + 1; i++)  
  89.             {  
  90.                 if (s % i == 0)  
  91.                 {  
  92.                     return false;  
  93.                 }  
  94.             }  
  95.  
  96.             return true;  
  97.         }  
  98.     }  
  99.  

clipboard

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值