文章目录
前言
继上一篇背包问题–贪心算法,这一次我们来看看动态规划
对于动态规划,一开始看背包问题的时候直接接触的是动态规划
感觉也是比较简单的
题目的具体要求,详细看上一篇,这一篇我们讲述另一个方法
一、动态规划
动态规划需要我们找到最优解,并且可以避免重复的计算,节省时间
动态规划分析
**开始,我们先定义一些声明和变量
Vi表示第 i 个物品的价值,Wi表示第 i 个物品的体积
定义V(i,j):当前背包容量 j,前 i 个物品最佳组合对应的价值
**
背包问题的动态规划,考虑的是 装和不装的问题,就是01背包问题
所以出现两种情况:
当不装的时候(背包容量比物体的重量小)此时的价值与前i-1个的价值是一样的,即V(i,j)=V(i-1,j)
当装入背包,但是可能到达不了最优,也要考虑装和不装
即V(i,j)=max{V(i-1,j),V(i-1,j-w(i))+v(i)}
选取最大值,保证最优
V(i-1,j)表示不装,V(i-1,j-w(i))+v(i) 表示装了第i个商品,背包容量减少w(i),但价值增加了v(i)
二、代码实现
1.主布局
相对于上次的贪心算法的布局,这次增加了动态规划
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="背包容量"
android:imeOptions="actionDone"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<!--
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:hint="物品个数"/>
</com.google.android.material.textfield.TextInputLayout>
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="物品重量"
android:imeOptions="actionNext"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/values"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="物品价值"
android:imeOptions="actionNone"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/define"
style="@style/materialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="#CC9933"
app:cornerRadius="12dp"
app:strokeColor="#CC9933"
app:strokeWidth="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:text="当前物品个数:"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/greed"
style="@style/materialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="贪心算法"
android:textColor="#CC9933"
app:cornerRadius="12dp"
app:strokeColor="#CC9933"
app:strokeWidth="2dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/dynamic"
style="@style/materialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态规划"
android:textColor="#CC9933"
app:cornerRadius="12dp"
app:strokeColor="#CC9933"
app:strokeWidth="2dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/goodsRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这一次我还新增了一个物品的个数的显示,比较方便提醒现在有多少个物品
2.布局
这一个布局呢 是点击了按钮之后跳转活动的布局
这一次动态规划呢 我稍微对布局显示的东西写的复杂了一点
写了两个RecyclerView,不过其实大致是差不多的东西内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".page.DynamicProgramming">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="背包容量:"
android:textColor="@color/black"
android:textSize=