回溯算法

      回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。回溯法是一种选优


  搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。许多复杂的,规模较大的问题都可以使用回溯法,有“通用解题方法”的美称。


  回溯算法也叫试探法,它是一种系统地搜索问题的解的方法。


  用回溯算法解决问题的一般步骤:


  1、针对所给问题,定义问题的解空间,它至少包含问题的一个(最优)解。


  2、确定易于搜索的解空间结构,使得能用回溯法方便地搜索整个解空间。


  3、以深度优先的方式搜索解空间,并且在搜索过程中用剪枝函数避免无效搜索。


  确定了解空间的组织结构后,回溯法就从开始结点(根结点)出发,以深度优先的方式搜索整个解空间。这个开始结点就成为一个活结点,同时也成为当前的扩展结点。在当前的扩展结点处,搜索向纵深方向移至一个新结点。这个新结点就成为一个新的活结点,并成为当前扩展结点。如果在当前的扩展结点处不能再向纵深方向移动,则当前扩展结点就成为死结点。此时,应往回移动(回溯)至最近的一个活结点处,并使这个活结点成为当前的扩展结点。回溯法即以这种工作方式递归地在解空间中搜索,直至找到所要求的解或解空间中已没有活结点时为止。


  回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试。

例题:

蓝桥杯    凑算式


AC:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int res = 0;
double a[11];
int b[11];
void dfs(int x)
{
	if(x == 10)
	{
		if(a[1] + a[2]/a[3] + (a[4]*100+a[5]*10 + a[6])/(a[7]*100 + a[8]*10+a[9]) == 10)//递归边界,当符合条件时停止
		{
			res++;
		}
		return;
	}
	else
	{
		for(int i = 1;i <= 9;i++)
		{
			if(!b[i])
			{
				b[i] = 1;
				a[x] = i;
				dfs(x + 1);
				b[i] = 0;
			}
		}
	}
	/*运用DFS遍历检察树,根据二叉树节点进行遍历,遍历过标记为1,最后别忘了标记为0,进行下一个节点的运算*/
}
int main()
{
	dfs(1);//直接用回溯一个节点就行
	cout << res << endl;
	return 0;	
}

答案:29

例题2(这个就难点了)HDU-1016

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): 61584    Accepted Submission(s): 26570


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

AC:

#include <iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int a[21];
int b[21];
int res[21];
int m;
void dfs(int x)
{
    if(x == m && !res[a[0] + a[m-1]])//判断结束位置,尤其注意判断第一个和最后一个是否为素数
    {
        printf("1");
        for(int i = 1;i < m;i++)
        {
            printf(" %d",a[i]);
        }
        printf("\n");
    }
    else
    {
        for(int i = 2;i <= m;i++)
        {
            if(!b[i]&&!res[a[x-1] + i])//回溯查询某个数和前一个数的和是否是素数
            {
                b[i] = 1;
                a[x] = i;
                dfs(x + 1);
                b[i] = 0;
            }
        }
    }
}
int main()
{
    for(int i = 2;i <= 42;i++)//打表查询素数
    {
        if(!res[i])
        {
            for(int j = 2;i*j <= 42;j++)
            {
                res[j*i] = 1;
            }
        }
    }
    int t = 0;
    a[0] = 1;
    b[1] = 1;
    while(~scanf("%d",&m))
    {
        printf("Case %d:\n",++t);
        dfs(1);
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值