01背包问题

这篇博客介绍了背包问题的三种动态规划解法:穷举法、自顶向下的动态规划和自底向上的动态规划。通过实例展示了如何在限定总质量下,求解放入物品以获得最大价值的问题。博客提供了C#代码实现,并使用了备忘录来优化自顶向下和自底向上的解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01背包问题有三种解法,穷举法,自顶向下的动态规划,自底向上的动态规划。

穷举法,要用到进制转换以及位运算符。

自底向下总result[m,i],相当于质量为m时放第i件物品时的最大价值。
列如result[5,2],就是质量为5,放了前两件物品时的最大价值。

当一个物品进来时有几种情况

如果如果进来的物品
w[i]>m放不下 result[m,i] = result[m,i-1]
放下有两种情况
放入背包,不放入背包
放入 result[m,i]=result[m-w[i],i-1]+p[i] (把该物品放入的话,先把这个物品的价值加上,然后加上不放入这个物品的最大价值)
不放入 result[m,i]=result[m,i-1]相当于这个物品没放
然后比较放和不放的价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 背包问题
{
    class Program
    {
        //备忘录用来保存数据 m i 最大价值
        static int[,] arry= new int [200,8];

        static int[,] result = new int[200, 8];

        static void Main(string[] args)
        {
            //int a = 30;
            //int b = 8;
            //Console.WriteLine(a & b);
            //Console.Read();

            int m;
            int[] w = { 0, 10, 40, 30, 50, 35, 40, 30 };
            int[] p = { 0, 35, 30, 6, 50, 40, 10, 25 };



            Console.WriteLine(Exhaustivity(150, w, p));

            Console.WriteLine(UpToDown(150, 7, p, w));

            Console.WriteLine(UpToDown2(150, 7, p, w));

            Console.WriteLine(BottomUp(150, 7, w, p));

            Console.ReadKey();

        }

        /// <summary>
        /// 穷举法 
        /// </summary>
        /// <returns></returns>
        static int Exhaustivity(int m,int []w,int []p)
        {
            int i = w.Length;
            int totalP = 0;
            int totalW = m;

            for (int j =1;j<= Math.Pow(2,i);j++)
            {
                int tempP = 0;
                int tempW = 0;

                for (int num=1;num<i;num++)
                {
                    

                    int result = Get2(j, num);

                    if(result==0)
                    {
                        continue;
                    }
                    else
                    {
                        tempP += p[num];
                        tempW += w[num];

                    }

                }

                if(tempP>totalP&&tempW<=totalW)
                {
                    totalP = tempP;
                }

            }
            return totalP;



        }

        /// <summary>
        /// 自顶向下不带备忘
        /// </summary>
        /// <param name="m"></param>
        /// <param name="i"></param>
        /// <param name="p"></param>
        /// <param name="w"></param>
        /// <returns></returns>
        static int UpToDown(int m,int i,int []p,int []w)
        {
           

            if(m==0||i==0)
            {
                return 0;
            }

            if (w[i] > m)
            {
               return UpToDown(m, i - 1, p, w);              
            }
            else
            {
                ///1不装 2装
                int temp1 = UpToDown(m, i - 1, p, w);
                int temp2 = UpToDown(m - w[i], i - 1, p, w) + p[i];

                if (temp1 > temp2)
                {
                    return temp1;
                }
                else
                {
                    return temp2;
                }



            }
            



        }

        static int UpToDown2(int m,int i,int []p,int []w)
        {
            if (m == 0 || i == 0)
            {
                return 0;
            }

            if(arry[m,i]!=0)
            {
                return arry[m, i];
            }

            if (w[i] > m)
            {
                arry[m, i] = UpToDown2(m, i - 1, p, w);
                return  arry[m, i];
            }
            else
            {
                ///1不装 2装
                int temp1 = UpToDown2(m, i - 1, p, w);
                int temp2 = UpToDown2(m - w[i], i - 1, p, w) + p[i];

                if (temp1 > temp2)
                {
                    arry[m, i] = temp1;
                    return arry[m,i];
                }
                else
                {
                    arry[m, i] = temp2;
                    return arry[m, i];                   
                }
            }
        }







        /// <summary>
        /// 把j传进来然后判断他的第num位是0还是1
        /// </summary>
        /// <param name="num"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        static int Get2(int j,int num)
        {
            int a=j;
            int b =(int) Math.Pow(2,num-1);

            int result = a & b;

            if(result==0)
            {
                return 0;
            }

            return 1;

        }

        static int BottomUp(int m, int i,int []w,int []p)
        {
            if (result[m, i] != 0)
            {
                return result[m, i];
            }


            for (int tempM=1;tempM<=m;tempM++)
            {
                for(int tempI=1;tempI<=i;tempI++)
                {
                    if(w[tempI]>tempM)
                    {
                        result[tempM, tempI] = result[tempM, tempI - 1];
                        
                    }

                    else
                    {
                        //1不放 2放
                        int temp1 = result[tempM, tempI - 1];
                        int temp2 = result[tempM - w[tempI], tempI-1]+p[tempI];

                        if(temp1>temp2)
                        {
                            result[tempM, tempI] = temp1;
                          //  return temp1;
                        }
                        else
                        {
                            result[tempM, tempI] = temp2;
                          //  return temp2;
                        }
                    }                   
                }               
            }
            return result[m, i];
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值