回溯法解裝載問題

2021-04-23 16:17:33 字數 595 閱讀 8706

遞迴解法

#include

using namespace std;

int bestw=0;

int cw=0;

int num=3;

int r=46;

int bestx[3];

void load(int *w,int c,int n,int *x)

r+=w[n];

}int main(void)

;int x=;

load(w,30,0,x);

cout《非遞迴解法

#include

using namespace std;

int maxloading( int w[ ],int c, int n, int bestx[ ] )

if( i == -1)

// 進入右子樹

x[ i ] = 0;

cw -= w[i];

i ++;}}

}    

int main()

;int bestx[3];

cout

}

回溯法,回溯法解裝載問題

利用回溯法解問題時一般按以下三步驟 1 定義問題的解空間 2 確定易於搜尋的解空間結構 3 以深度優先策略搜尋解空間,並在搜尋過程中用剪枝函式避免無效搜尋 二 回溯法應用 裝載問題 一批貨櫃共n個要裝上2艘載重量分別為c1和c2的輪船,其中貨櫃i的重量為wi且w1 w2 wn c1 c2 試確定乙個...

回溯法求解裝載問題

有n個貨櫃要裝上一艘載重量為w的輪船,其中貨櫃i 1 i n 的重量為wi。不考慮貨櫃的體積限制,現要從這些貨櫃中選出重量和小於等於w並且盡可能大的若干裝上輪船。例如,n 5,w 10,w 時,其最佳裝載方案是 1,1,0,0,1 或者 0,0,1,1,0 maxw 10。採用帶剪枝的回溯法求解。問...

回溯法解裝載問題 2演算法設計

2.演算法設計 include stdafx.h include include include using namespace std 演算法設計 template class loading template void loading backtrack int i 搜尋子樹 if cw w i...