0/1背包问题 - 回溯法(C++实现)
2017-06-26 17:25
603 查看
0/1背包问题 - 回溯法(C++实现)
flyfish
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate c (“backtracks”) as soon as it determines that c cannot possibly be completed to a valid solution
回溯法是查找计算问题的所有或者一些解决方案的通用算法。尤其constraint satisfaction problems(约束满足问题),逐步建立候选的解决方案,尽早抛弃无效的
解决方案
notably与particularly,especially是同义词
especially和specially之间的区别
通过实例区别
I liked all the children, Tom especially.我喜欢所有的孩子,特别是汤姆.
I don’t like bright color, especially red.我不喜欢鲜艳的颜色, 尤其是红色.
Those shoes were specially made for me.这双鞋是专门为我做的.
I made this specially for your birthday.这是我特意为你生日而做的
The ring was specially made for her.戒指是为她特制的
解决方案集合构成的一颗二叉树
![](https://img-blog.csdn.net/20170626214937253?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmx5ZmlzaDE5ODY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
代码:VC++调试通过
flyfish
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate c (“backtracks”) as soon as it determines that c cannot possibly be completed to a valid solution
回溯法是查找计算问题的所有或者一些解决方案的通用算法。尤其constraint satisfaction problems(约束满足问题),逐步建立候选的解决方案,尽早抛弃无效的
解决方案
notably与particularly,especially是同义词
especially和specially之间的区别
通过实例区别
I liked all the children, Tom especially.我喜欢所有的孩子,特别是汤姆.
I don’t like bright color, especially red.我不喜欢鲜艳的颜色, 尤其是红色.
Those shoes were specially made for me.这双鞋是专门为我做的.
I made this specially for your birthday.这是我特意为你生日而做的
The ring was specially made for her.戒指是为她特制的
解决方案集合构成的一颗二叉树
代码:VC++调试通过
#include "stdafx.h" #include <iostream> #include <algorithm> #include <vector> struct Item //物品定义 { int id, weight, value;//编号,重量,价值。编号为0的物品这里没有使用 Item(){} Item(int i, int w, int v) :id(i), weight(w), value(v){} void operator += (const Item &i) //重载操作符主要是为计算使用 { this->value = this->value + i.value; this->weight = this->weight + i.weight; } void operator -= (const Item &i) { this->value = this->value - i.value; this->weight = this->weight - i.weight; } }; Item currentItem{ 0, 0, 0 }; const int n = 4, C = 10; //C背包所能承受的最大重量 //物品个数n int edge ; //记录树中的路径,1表示装入背包,0反之 std::vector<Item> allItems;//所有的物品 int maxValue = 0;//能够装入背包的最大价值 void Backtracking(int i) { if (i >= n)//递归结束 { if (currentItem.value > maxValue) maxValue = currentItem.value; return; } if (currentItem.weight + allItems[i].weight <= C)//边为1的子节点 { edge[i] = 1; currentItem += allItems[i]; Backtracking(i + 1); currentItem -= allItems[i]; } else { edge[i] = 0;//边为0的子节点 Backtracking(i + 1); } } int _tmain(int argc, _TCHAR* argv[]) { allItems.push_back(Item(1, 3, 30)); allItems.push_back(Item(2, 5, 20)); allItems.push_back(Item(3, 4, 10)); allItems.push_back(Item(4, 2, 40)); Backtracking(0); for (int i = 0; i < n; i++) { if (edge[i] == 1) { std::cout << "物品编号:" << allItems[i].id << " 重量:" << allItems[i].weight << " 价值:" << allItems[i].value << std::endl; } } std::cout << "背包最大价值:" << maxValue; return 0; }
相关文章推荐
- 回溯法之0-1背包问题(C实现)
- 0/1背包问题 - 分枝定界(C++实现)
- 01背包问题 及c++ 代码实现
- 0/1背包问题 - 动态规划(C++实现)
- c++实现0-1背包问题完整源码(动态规划实现)
- c++实现0-1背包问题完整源码续(动态规划+回溯法实现)
- 拉丁矩阵问题 利用回溯法的C++实现方案
- C++ 实现 0-1 背包问题
- 0-1背包问题与完全背包问题C++实现 动态规划
- C++回溯法0/1背包问题DKNAP
- C++实现动态规划算法之解决0-1背包问题
- 贪心法和回溯法 求解“背包、0/1背包问题”——Java 实现
- dynamic programming --knapsack problem 背包问题(c++实现)
- 0-1背包问题与完全背包问题C++实现 动态规划
- 加了限制条件的0-1背包问题(C++实现)
- 01背包问题与分数背包问题 C++实现
- 0-1背包问题与完全背包问题C++实现 动态规划
- C++实现多目标遗传算法(0/1背包问题)
- 贪心算法运用于背包问题(C++实现)
- 01背包、完全背包、多重背包问题的C++实现及路径记录