Loading problem (backtracking)

1. Backtracking

First, let's introduce the backtracking method:

(1) Basic idea: Convert the solution space of the problem into a graph or tree structure representation, and then use the depth-first search strategy to traverse, record and find all feasible solutions or optimal solutions during the traversal process.

(2) Main steps: a. Solve the solution space where all solutions are located; b. Construct the corresponding tree and other data structures to represent the solution space; c. Use depth-first search to search for all optimal solutions in the tree (combined with pruning at the same time) function to improve search efficiency);

(3) Application: When the problem is to solve the optimal solution or all solutions that satisfy certain constraints, the backtracking method can be used, which has the reputation of "universal problem-solving method";

(4) Implementation: Both recursive and non-recursive methods are used for implementation, in which the recursive method is simple in design but low in efficiency, while the non-recursive design is more complex but high in efficiency;

There are two main types of tree structures used: subset tree and permutation tree ;

Second, the loading problem

Problem description:
There is a batch of n containers to be loaded on a ship with a deadweight c, where the weight of container i is wi. Find an optimal loading plan to fill the ship as much as possible, that is, load the heaviest container on the ship under the condition that the loading volume is not limited.

code show as below:

//load.h
#ifndef LOAD_H
#define LOAD_H #include "load.template" #endif

 

//load.template
template <class Type> class Loading { friend Type Maxloading(Type[], Type, int , int []); private : void Backtrack( int i); int n, // Number of containers *x, // Current solution *bestx; // Current optimal solution Type *w, // Container weight array c, // On-load weight of the first ship cw, // Current deadweight bestw, // Current optimal loading weight r; // Remaining container weight }; template <class Type> void Loading<Type> ::Backtrack(int i) { // Search for the i-th layer node if (i > n) // reach the leaf node { if (cw > bestw) { for (int j = 1; j <= n; j++) bestx[j] = x[j]; bestw = cw; } return; } // search subtree r -= w[i]; if (cw + w[i] <= c) // search left subtree { x[i] = 1; cw += w[i]; Backtrack(i + 1); cw -= w[i]; } if (cw + r > bestw) // search the right subtree { x[i] = 0; Backtrack(i + 1); } r += w[i]; // backtrack } template <class Type> Type Maxloading(Type w[], Type c, int n, int bestx[]) { // return the optimal weight Loading <Type> X; // initialize X Xx = new int [n + 1 ]; X.w = w; X.c = c; X.n = n; x.bestx = bestx; X.bestw = 0 ; X.cw = 0; //初始化r X.r = 0; for (int i = 1; i <= n; i++) { X.r += w[i]; } X.Backtrack(1); delete[] X.x; return X.bestw; }

 

// 2018_04_28
 // Use the backtracking method to solve the loading problem 
//main.cpp
// ================================= =============================================== #include <iostream > #include " load.h " using namespace std; int main(void) { int n = 0 ; // number of containers; int c = 0 ; // load of the first ship int bestx[ 4 ]; // best solution int *w = new int [n + 1 ]; // container Weight array int m = 0 ; // Optimum load cout << " Please enter the number of containers: " ; cin >> n; cout << endl; cout << " Please enter the weight of the first ship: " ; cin >> c; cout << endl; cout << " Please enter the weight array of the container: " ; for ( int i = 0 ; i <= n; i++ ) { cin >> w[i]; } m = Maxloading(w, c, n, bestx); // Solve the problem cout << "The output optimal solution is as follows: " << endl; for ( int i = 1 ; i < n; i++ ) { cout << bestx[i] << " "; } cout << " Please output the optimal load: " << endl; cout << m; system("pause"); return 0; }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325056517&siteId=291194637