五、算法_动态规划_背包01

来源:互联网 发布:霍金对人工智能的看法 编辑:程序博客网 时间:2024/05/17 08:19

动态规划:背包问题01位

  问题:有一个m KG的背包和物品 {T1T2T3Tn},每件物品价格{P1P2P3Pn},现在要把所有最大价值的物品放进背包中,且每个物品只能放入一次(不能超出背包重量),其中所有的条件都是已知的,求最大价值;

穷举法:

using System;namespace 动态规划_背包穷举 {    class Program {        static void Main (string[] args) {            int pMKG ;            int[] pWkg = new[] { 0, 3, 4, 5 };            int[] pPrices = new[] { 0, 4, 5, 6 };            Console.WriteLine(Exhaustivity(10, pWkg, pPrices));            Console.WriteLine(Exhaustivity(3, pWkg, pPrices));            Console.WriteLine(Exhaustivity(4, pWkg, pPrices));            Console.WriteLine(Exhaustivity(5, pWkg, pPrices));            Console.WriteLine(Exhaustivity(7, pWkg, pPrices));            Console.WriteLine();            Console.ReadKey();        }        //  ------------穷举法------------        public static int Exhaustivity (int mKg, int[] wKg, int[] prices) {            //  物品个数            int mRefI = wKg.Length-1;            int pMaxPrices = 0;            for (int i = 0; i < Math.Pow(2, mKg); i++) {                //  重量和                int pWeightTotal = 0;                //  价格和                int pPricesTotal = 0;                //  取得 i 上某一位的二进制值                for (int j = 1; j <= mRefI; j++) {                    int pResult = Get(i, j);                    if (pResult == 1) {                        //  物品的价格和重量                        pWeightTotal += wKg[j];                        pPricesTotal += prices[j];                    }                }                //  设置最大价值,和判断背包是否放得下物品                if (pWeightTotal <= mKg && pPricesTotal > pMaxPrices) {                    pMaxPrices = pPricesTotal;                }            }            return pMaxPrices;        }        //  取得 i 上第几位上的二进制值,是1或者0        //  按位与运算        public static int Get (int pNumber, int pIndex) {            int a = pNumber;            int b = (int)Math.Pow(2, pIndex - 1);            int pRef = a & b;            if (pRef == 0) {                return 0;            }            return 1;        }    }}

动态规划(自底向上):

namespace 动态规___自底向上 {    class Program {        static void Main (string[] args) {            int pMKG;            int[] pWkg = new[] { 0, 3, 4, 5 };            int[] pPrices = new[] { 0, 4, 5, 6 };            Console.WriteLine(BottomUp(10,3, pWkg, pPrices));            Console.WriteLine(BottomUp(3,3, pWkg, pPrices));            Console.WriteLine(BottomUp(4,3, pWkg, pPrices));            Console.WriteLine(BottomUp(5,3, pWkg, pPrices));            Console.WriteLine(BottomUp(7,3, pWkg, pPrices));            Console.WriteLine();            Console.ReadKey();        }        public static int[,] pResult = new int[11, 4];        public static int BottomUp (int mKg, int pIndex, int[] wKg, int[] pPrices) {            if (pResult[mKg, pIndex] != 0)                return pResult[mKg, pIndex];            for (int i = 0; i < mKg + 1; i++) {                for (int j = 1; j < pIndex + 1; j++)                {                    if (pResult[i, j] != 0) continue;                    if (wKg[j] > i) {                        pResult[i, j] = pResult[i, j - 1];                    } else {                        int pMaxValue1 = pResult[i - wKg[j], j - 1] + pPrices[j];                        int pMaxValue2 = pResult[i, j - 1];                        if (pMaxValue1 > pMaxValue2) {                            pResult[i, j] = pMaxValue1;                        } else {                            pResult[i, j] = pMaxValue2;                        }// End if                    }// End if                }            }// End for            return pResult[mKg, pIndex];        }// End bottomup    }}
0 0