Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backtracking #49

Open
shanejix opened this issue Aug 6, 2021 · 0 comments
Open

Backtracking #49

shanejix opened this issue Aug 6, 2021 · 0 comments

Comments

@shanejix
Copy link
Owner

shanejix commented Aug 6, 2021

回溯是建立在 DFS 的基础之上的

回溯模板

const res = []

// path 路径
const target = []

const dfs = (source, deep, ...) => { // 状态变量 

    // 递归终止的条件
    if (🤔) {
        // 存储满足条件的结果
        res.push(target.slice())
        // 返回
        return
    }

    // 当前节点 for loop 横向遍历,向下分叉, index 视情况而定
    for (let i = index; i < source.length; i++) {

        // 是否需要剪枝

        // 做出选择
        target.push(source[i]);

        // 深度递归,进入下一层
        dfs(source, 🤔 + 1, ...);

        // 撤销选择
        target.pop();
    }

    return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant