回溯法 01背包
来源:互联网 发布:steam无法连接到网络 编辑:程序博客网 时间:2024/05/16 16:19
问题太经典,就不描述问题了,以前都是用动态规划做的,blog上也有,现在看看回溯法的程序:
/** * 01背包的回溯解法 */public class BeiBao01 {static int c; //背包容量static int n; //对象数目static int[] w; //对象重量数组static int[] p; //对象收益数组static int cw; //当前背包重量static int cp; //当前背包价值static int bestp;//迄今最大的收益static int[] X; //记录在书中的移动路径public static void main(String args[]) {c=30;n=3;w=new int[]{20,15,15};p=new int[]{40,25,25};X=new int[3];getBest(0);System.out.println(bestp);}public static void getBest(int i){if(i>=n){if(cp>bestp){bestp=cp;}return;}if(cw+w[i]<=c){X[i]=1;cw+=w[i];cp+=p[i];getBest(i+1);cw-=w[i];cp-=p[i];}X[i]=0;getBest(i+1);}}
- 回溯法 01背包
- 01背包回溯法
- 回溯法 01背包问题
- 01背包(回溯法)
- 01背包------回溯法(包括回溯法讲解)
- 回溯法之01背包问题
- 01背包问题-----回溯法的解决方案
- Java之回溯法实现01背包
- 回溯法求解01背包问题
- 01背包问题(回溯法实现,java)
- 回溯法-经典 01背包问题
- 回溯法之01背包问题
- 回溯法——01背包问题
- 回溯法求解01背包问题
- 回溯法解决01背包问题
- 回溯法 01背包问题 剪枝
- 回溯法 普通01背包问题
- 回溯法实现01背包问题
- iOS6定位服务编程详解
- hibernate+struts2登录 用户名 密码 判断问题
- C++中class和include的区别
- uva10308 - Roads in the North 树形DP
- Developing Excel Utilities with VBA
- 回溯法 01背包
- 第14周项目2-改变数组元素值
- C语言用结构体指针作函数参数
- 嘻嘻嘻
- iOS开发那些事-iOS6苹果地图实用开发
- Mysql启动失败报错1067
- sgu 134 Centroid 树的重心
- chrome扩展下载地址 --- url模版
- leetcode-Symmetric Tree