1、概念
回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。
回溯法是一种选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。
...
分类:
其他好文 时间:
2015-04-06 15:48:22
阅读次数:
254
一:Unique paths I
题目:
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.The solution set must not contain duplic...
分类:
其他好文 时间:
2015-04-06 15:44:07
阅读次数:
122
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:
在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上
(斜率为1),问有多少种摆法。高斯认为有76种方案。
1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。
计算机发明后,有多种方法可...
分类:
编程语言 时间:
2015-04-06 14:15:43
阅读次数:
144
1.题目描述:点击打开链接
2.解题思路:本题利用回溯法解决。首先生成2*n范围内的所有素数,便于后续的判断。接下来试着填写每一位,如果数字i满足没有用过且它与前一项之和是素数,那么就可以用它,同时标记它已使用,递归寻找cur+1,退出时清除使用标记。
3.代码:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#inc...
分类:
其他好文 时间:
2015-04-06 08:56:55
阅读次数:
123
1.题目描述:点击打开链接
2.解题思路:本题利用回溯法解决。根据题意描述,易知在枚举第cur位时,只用检查它添加后该串是否合法,而不必去检查cur之间的串是否合法,因为这一步检查在枚举cur之前早已经做过了。
3.代码:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#includ...
分类:
其他好文 时间:
2015-04-06 08:55:51
阅读次数:
122
一: Plus One
题目:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
...
分类:
其他好文 时间:
2015-04-05 21:59:23
阅读次数:
159
题目链接:restore-ip-addresses
import java.util.ArrayList;
import java.util.List;
/**
*
Given a string containing only digits,
restore it by returning all possible valid IP address combination...
分类:
其他好文 时间:
2015-04-04 09:14:43
阅读次数:
113
Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated num...
分类:
其他好文 时间:
2015-04-04 06:38:53
阅读次数:
143
题目链接:word-search
/**
*
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell,
where "adjacent" cells...
分类:
其他好文 时间:
2015-04-03 09:19:28
阅读次数:
148
题目链接:http://poj.org/problem?id=2367
题目大意:就是进行拓扑排序,先给你一个数n,代表1~n,对于每个数有一系列的指向,最后将这些数进行排列出来。。就是简单的拓扑排序。
首先拓扑排序应该有两种实现的方法。。
一种是用dfs进行每个节点的搜索,最后进行回溯,这样的话很容易就能明白先找出来的应该是后面的数,而最后找出来的应该是之前的数,因为是回溯出来的嘛。。所以...
分类:
编程语言 时间:
2015-04-01 22:07:50
阅读次数:
168