C++ algorithm learning (branch and bound method)

1. Goal

Find the optimal solution under constraints

2. Method

Search the solution space tree in a breadth-first or least-cost-first manner.

3. Concrete realization

In the branch and bound method, each live node has a chance to become an extended node, generating all its son nodes at once. Inappropriate son nodes are eliminated, leaving only suitable ones entering the queue, and the previous ones leaving the queue. I personally feel that this is BFS, and there are other understandings that can be discussed.

4. Example questions:

I suggest you write it by hand. Although it is a personal note, I hope it will be useful to netizens.

1. The basis of breadth-first traversal

(1) Force button: Sword refers to Offer 32-I. Print the binary tree from top to bottom

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<int> levelOrder(TreeNode* root) {
    
    
     if(!root) return {
    
    };
     queue<TreeNode *> mes;
     vector<int> answer;
     mes.push(root);
     while(!mes.empty()){
    
    
         TreeNode * node = mes.front();
         mes.pop();
         if(node->left) mes.push(node->left);
         if(node->right) mes.push(node->right);
         answer.push_back(node->val);
     }
     return answer;
    }
};

(2) Force button: Sword refers to Offer 32-II. Print the binary tree from top to bottom II

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
    
    
    if(!root) return {
    
    };
    vector<vector<int>> a;
    queue<TreeNode*> data;
    data.push(root);
    while(data.size()){
    
    
        int num = data.size();
        vector<int> ai;
        for(int i = 0;i < num;i++){
    
    
            TreeNode * node = data.front();
            data.pop();
            if(!node) continue;
            if(node->left) data.push(node->left);
            if(node->right) data.push(node->right);
            ai.push_back(node->val);
        }
        if(ai.size() != 0) a.push_back(ai);
    }
    return a;
    }
};

(2) Force button: Sword refers to Offer 32-III. Print binary tree III from top to bottom

This is actually the same as before. This is a way to introduce you:

v.insert(v.begin(),data);//把data插入到vector的最前面
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
    
    
    if(!root) return {
    
    };
    vector<vector<int>> a;
    queue<TreeNode*> data;
    data.push(root);
    int floor = 0;
    while(data.size()){
    
    
        floor++;
        int num = data.size();
        vector<int> ai;
        for(int i = 0;i < num;i++){
    
    
            TreeNode * node = data.front();
            data.pop();
            if(!node) continue;
            if(node->left) data.push(node->left);
            if(node->right) data.push(node->right);

            if(floor%2 == 0) ai.insert(ai.begin(),node->val);
            else ai.push_back(node->val);
        }
        if(ai.size() != 0) a.push_back(ai);
    }
    return a;
    }
};

2. Use breadth-first traversal

(1) Force button: 542.01 matrix

class Solution {
    
    
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
    
    
        //准备工作
        queue<pair<int, int>> q;
        int row = matrix.size();
        int col = matrix[0].size();
        //开始第一步把0放入队列
        for(int i = 0;i < row;i++)
        for(int j = 0;j < col;j++){
    
    
            if(matrix[i][j] == 0)  q.push({
    
    i,j});
            else matrix[i][j] = -1;
        }
        //开始插入
        int a[] = {
    
    -1,1,0,0};
        int b[] = {
    
    0,0,1,-1};
        while(!q.empty()){
    
    
            pair<int,int> pair_data = q.front();
            q.pop();
            for(int i = 0;i <4;i++){
    
    
                int newX = pair_data.first + a[i];
                int newY = pair_data.second + b[i];
                if(newX >= 0 
                && newX < row 
                && newY >= 0
                && newY < col
                && matrix[newX][newY] == -1)
                {
    
    
                    matrix[newX][newY] = matrix[pair_data.first][pair_data.second] + 1;
                    q.push({
    
    newX,newY});
                }
                }
            }
        return matrix;
    }
};

It’s a bit of a cloud on the Internet, so I don’t write it yet. I will update it when I encounter branch boundaries in the future. It is mainly used for personal notes. I hope it will help you a little bit.

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/110704345