递归回溯算法

某个递归树如下:
子集问题递归树.png由123集合中挑选出空,1,2,3,这个横向拆分的过程就是在进行for循环,最初index肯定是0,我们称这个为第一层。

for (int i = index; i < nums.length; i++) {}

而纵向往下的过程就是在递归,递归时也有横向的for循环,这时候就要控制for循环的起始点也就是index的起始值(大部分编程题就是在控制index的选择方式来控制递归树往预期的方向生成)。

1、index如果选择index+1,代表上一层的for循环每条分支往下一层走时都从固定的index+1开始,则不管横向for循环走到哪里,纵向看每一层所有分支对应的index的深度都相等。

所以如果上一层for循环到3了,index还在2,就会出现3,2的情况。

不进行其他操作时的递归树为:

index在1:1;2;3

index在2:12,13;22,23;32,33;

index在3:123,133;223,233;323,333

index在n:。。。。。。

适合要求结果集为等长且元素不重复的场景,如123求排列123,132,213,231,312,321等。

输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

img

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        for (int num : nums) {
            temp.add(num);
        }
        permute(nums.length, 0);
        return res;
    }

    public void permute(int length, int index) {
        // 递归结束条件,index走到temp最后一个元素,没有别的情况直接add
        if (index == length - 1) {
            res.add(new ArrayList<Integer>(temp));
            return;
        }
        // 递归
        for (int i = index; i < length; i++) {
            // 把i对应元素和index对应元素互换
            Collections.swap(temp, index, i);
            permute(length, index + 1);
            Collections.swap(temp, index, i);
            // 把i对应元素和index对应元素互换
        }
    }
}

2、index如果选择index,index永远涨不了,相当于往下一层走时都可以从数组头部开始重新遍历。

不进行其他操作时的递归树为:

index在1:1;2;3

index在1:11,12,13;21,22,23;31,32,33

index在1:111,112,113;121,122,123;131,132,133;211,212,213;221,222,223;231,232,233;

311,312,313;321,322,323;331,332,333

index在n:。。。。。。

3、index如果选择i,上一层的for循环每条分支往下一层走时,可以重复上次for循环的分支停留的位置继续走

index在1:1;2;3

index在1:11,12,13;index在2:22,23;index在3:33

index在1:111,112,113;index在2:122,123;222,223;index在3:133,233,333

index在n:。。。。。。

适合求硬币问题的场景,每个硬币可以使用多次

输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]

public class Solution {
    List<List<Integer>> res = new ArrayList<>();
    Deque<Integer> path = new LinkedList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if (candidates.length == 0) {
            return res;
        }

        Arrays.sort(candidates);
        dfs(candidates, 0, target);
        return res;
    }

    private void dfs(int[] candidates, int index, int target) {
        if (target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = index; i < candidates.length; i++) {
            if (candidates[i] > target) {
                break;
            }
            path.addLast(candidates[i]);
            // 由于每一个元素可以重复使用,下一轮搜索的起点依然是 i
            dfs(candidates, i, target - candidates[i]);
            path.removeLast();
        }
    }
}

4、index如果选择i+1,上一层的for循环每条分支往下一层走时,接着上次for循环的分支停留的位置往后继续走

index在1:1;2;3

index在2:12,13;index在3:23

index在3:123;

index在n:。。。。。。

适合当前求子集的场景。

输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        backtrack(0, nums);
        return res;
    }

    public void backtrack(int index, int[] nums) {
        res.add(new ArrayList<>(temp));
        for (int i = index; i < nums.length; i++) {
            temp.add(nums[i]);
            backtrack(i + 1, nums);
            temp.remove(temp.size() - 1);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
递归回溯算法是一种常用于生成迷宫的算法。它通过不断地递归调用自身在迷宫中移动,并在每一步中做出选择,直到达到终点或无法继续移动为止。 在使用递归回溯算法生成迷宫时,需要遵循以下步骤: 1. 创建一个迷宫的二维数组,用于表示迷宫的结构。通常使用0表示可通行的路径,1表示墙壁。 2. 选择一个起始点,将其设为当前位置。 3. 在当前位置,随机选择一个方向(上、下、左、右)。 4. 根据选择的方向,判断下一个位置是否可通行。如果可通行,则移动到下一个位置,并将当前位置标记为已访问。 5. 递归调用自身,在新的位置上重复步骤3和步骤4,直到无法继续移动。 6. 当无法继续移动时,返回到上一个位置,并选择下一个方向继续尝试。 7. 当所有的路径都被访问过后,迷宫生成完成。 通过不断地递归调用自身,递归回溯算法能够探索迷宫中的所有可能路径,并生成一个完整的迷宫。这种算法的关键是在每一步中做出选择,并保存上一步的状态,以便在无法继续移动时回溯到上一步。 递归回溯算法的优点是可以生成迷宫的所有路径,并且在实现上比较简单。然而,由于递归的特性,当迷宫规模较大时,可能会导致栈溢出的问题。为了解决这个问题,可以使用迭代的方式来实现迷宫生成算法,或者采用其他优化技巧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [递归-迷宫问题(回溯)](https://blog.csdn.net/fyj13925475957/article/details/103677376)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [[python实现] 递归回溯(深度优先)构造随机迷宫](https://blog.csdn.net/qq_39391544/article/details/121306611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值