backtracking及其应用
文章目录
- 应用场景
- N-Queens
- Permutations
- Permutations II
- 参考资料
backtracking(回溯法)是一种算法,主要用来解决带限制条件的计算问题( CSP)。
特点如下:
- 和暴力匹配算法一样,会尝试所有的可能性。
- 比暴力匹配算法好,会在尝试的过程中不断丢弃不正确的解。
应用场景
N-Queens
链接:https://leetcode.com/problems/n-queens/
代码
func solveNQueens(n int) [][]string {board := make([][]int, n)for i := 0; i < n; i++ {board[i] = make([]int, n)}var res [][]stringres = solve(res, board, 0)return res
}// row: 当前行
func solve(res [][]string, board [][]int, row int) [][]string {if row == len(board) {res = append(res, construct(board))return res}for col := 0; col < len(board); col++ {if check(board, row, col) {board[row][col] = 1res = solve(res, board, row+1)board[row][col] = 0}}return res
}func check(board [][]int, row int, col int) bool {for i := 0; i < row; i++ {if board[i][col] == 1 {return false}rowDiff := row - i// 45度if col + rowDiff < len(board) && board[i][col + rowDiff] == 1 {return false}// 135度if col - rowDiff >= 0 && board[i][col - rowDiff] == 1 {return false}}return true
}func construct(board [][]int) []string {var res []stringfor i := 0; i < len(board); i++ {var tmp stringfor j := 0; j < len(board); j++ {if board[i][j] == 1 {tmp += "Q"} else {tmp += "."}}res = append(res, tmp)}return res
}
Permutations
链接:https://leetcode.com/problems/permutations/
代码
func permute(nums []int) [][]int {var res [][]intboard := make([]int, len(nums))for i := 0; i < len(board); i++ {board[i] = math.MinInt32}res = solve(res, board, nums, 0)return res
}func solve(res [][]int, board []int, nums []int, index int) [][]int {if index == len(board) {res = append(res, construct(board))return res}for col := 0; col < len(board); col++ {if check(board, col) {board[col] = nums[index]res = solve(res, board, nums, index+1)board[col] = math.MinInt32}}return res
}func check(board []int, col int) bool {return board[col] == math.MinInt32
}func construct(board []int) []int {res := make([]int, len(board))for i := 0; i < len(board); i++ {res[i] = board[i]}return res
}
同样是回溯,这道题还有另一种解法
func permute(nums []int) [][]int {var res [][]intused := make([]bool, len(nums))var tmpList []intres = backtrack(res, tmpList, used, nums)return res
}func backtrack(res [][]int, tmpList []int, used []bool, nums []int) [][]int {if len(tmpList) == len(nums) {res = append(res, construct(tmpList))return res}for i := 0; i < len(nums); i++ {if used[i] {continue}used[i] = truetmpList = append(tmpList, nums[i])res = backtrack(res, tmpList, used, nums)tmpList = tmpList[0:len(tmpList)-1]used[i] = false}return res
}func construct(tmpList []int) []int {res := make([]int, len(tmpList))for i := 0; i < len(tmpList); i++ {res[i] = tmpList[i]}return res
}
这里简单说下两者的区别
解法1:
参考N-Queens,题目演化为一个1x3的棋盘,然后将nums中的元素逐个放到棋盘的第一列、第二列、第三列。
解法2:
符合正常思路,遍历nums,将元素逐个放到一个list中。
对于Permutations,我觉得解法1和解法2都是OK的,但是对于Permutations II,解法1扩展起来就比较困难了。
Permutations II
链接:https://leetcode.com/problems/permutations-ii/
代码
【仅贴出和Permutations不同的地方】
参考资料
https://www.interviewbit.com/courses/programming/topics/backtracking/
https://leetcode.com/problems/permutations/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning)
https://leetcode.com/problems/permutations-ii/discuss/18594/Really-easy-Java-solution-much-easier-than-the-solutions-with-very-high-vote
backtracking及其应用相关推荐
- [LeetCode] 93. Restore IP Addresses_Medium tag: backtracking
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 数据结构与算法(Python)– 回溯法(Backtracking algorithm)
数据结构与算法(Python)– 回溯法(Backtracking algorithm) 1.回溯法 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条 ...
- JavaScript实现backtracking Jump Game回溯跳跃游戏算法(附完整源码)
JavaScript实现backtracking Jump Game回溯跳跃游戏算法(附完整源码) backtrackingJumpGame.js完整源代码 backtrackingJumpGame. ...
- JavaScript实现使用 BACKTRACKING 方法查找集合的幂集算法
JavaScript实现使用 BACKTRACKING 方法查找集合的幂集算法(附完整源码) btPowerSet.js完整源代码 btPowerSet.test.js完整源代码 btPowerSet ...
- LeetCode——Backtracking
Backtracking 目录 Backtracking 数字键盘组合 IP 地址划分 在矩阵中寻找字符串 输出二叉树中所有从根到叶子的路径 排列 含有相同元素求排列 组合 组合求和 含有相同元素的组 ...
- backtracking算法
详解backtracking 搞清楚回溯体,穷举多维度值,维度一定会加一 数值一定很小.搞清楚怎么处理输入输出. 有一定的模版. uva,112 这种思想和获取方式是我所追求的. 1 #include ...
- 递归算法(三)- 回溯法Backtracking
回溯法 回溯法Backtracking(找所有的可能)递归: 类似枚举,一层一层向下递归,尝试搜索答案. 找到答案: => 返回答案,并尝试别的可能 未找到答案: => 返回上一层递归,尝 ...
- 回溯法|Backtracking
回溯法 回溯是一种算法,用于捕获给定计算问题的部分或全部解决方案,特别是约束满足问题.该算法只能用于能够接受"部分候选解"概念的问题,并允许快速测试候选解是否可以是一个完整的解.回 ...
- Handbook of Constraints Programming——Chapter4 Backtracking Search Algorithms-Preliminaries
来源:F.Rossi, P.Van Beek, T. Walsh. Handbook of Constraints Programming. Elsevier, 2006. There are thr ...
- 【DFS + Backtracking】LeetCode 79. Word Search
LeetCode 79. Word Search Solution1:我的答案 DFS + Backtracking class Solution { public:bool exist(vector ...
最新文章
- Java8新特性学习记录
- FlexViewer入门资料
- 使用阿里云容器服务Kubernetes实现蓝绿发布功能
- 在SAP C4C创建销售订单时如何绑定创建者的微信open id
- EasySQLMAIL中企业微信的配置方法
- matlab滤波器函数6,matlab中滤波器函数filter的c语言实现
- p坚持csma协议 仿真‘_巧家快速推进移民生产安置人口界定和协议签订工作
- Windows系统、下的MySQL、版本升级、实操
- cmd 卸载mysql_彻底卸载MySQL图文教程
- 解决:qrc文件中删除资源文件后编译失败
- HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
- [eNSP]→ospf基本配置、区域划分
- electron (二) 暗黑模式
- 迅歌KTV服务器各型号,2017年ktv必点歌曲排行榜(4页)-原创力文档
- 从零开始学习UCOSII操作系统2--UCOSII的内核实现
- caffe的concat层
- 基于eBPF的云原生可观测性开源工具Kindling之Kindling-agent 性能测试评估
- 神盾特工hive_《神盾局特工》第四季剧情:黛西黑化?
- Macbook M1芯片软件记录
- Github教程】史上最全github使用方法:github入门到精通
热门文章
- 【AI测试】人工智能测试整体介绍——第三部分
- 利用百度(或者360搜索等)的快照解决公司网络限制
- Tasker实现的app界面实时翻译 - 界面翻译4.0
- 推荐两款简单好用的扒站工具
- fect:基于面板数据的因果推断(上)
- Haproxy基础知识
- 【spring_Cloud】java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud
- 手写签名插件—jSignature使用心得
- matlab2012a到期重新激活
- 【光线追踪系列九】物体动态模糊