• 大小: 156KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-07-19
  • 语言: C#
  • 标签: 动态规划  算法  C#  

资源简介

01背包问题(C#图形界面),用动态规划的算法实现,算法设计与分析的上机作业,分享给各位,相互学习。

资源截图

代码片段和文件信息

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


namespace WindowsFormsApplication1
{
    class BagProblem
    {
       public int[] profit;//物品价值
       public int[] weight;//物品重量
       public int numbers;//物品个数
       public int capacity;//背包载重
       public int[] d; //最优决策数组

        /// 
        /// 向前回溯,确定所选的物品,记录在x[]数组中
       /// 最优决策数组
       /// 用0、1记录物体选取情况
        /// 

       public void HuiSu(int[] D int[] Weight int Capacity int[] x)
        {
            numbers = Weight.Length - 1;
            for (int i = 0; i < numbers; i++)
                if (d[i Capacity] == 1)
                {
                    x[i] = 1;
                    Capacity -= Weight[i];
                }
                else
                    x[i] = 0;
            x[numbers] = d[numbers Capacity];
        }
         
        /// 
        /// 求最大价值
        /// 物体价值
        /// 物体重量
        /// 背包载重
        /// 

        public  int MaxProfit(int[] Profit int[] Weight int Capacity)
        {
            profit = Profit;
            weight = Weight;
            numbers = Profit.Length - 1;
            return f(0 Capacity);
        
        }

        /// 
        /// 根据递推关系求解
        /// 

        private int f(int i int j)
        {
            if (i == numbers)//如果只有一个物品
            {
                if (j < weight[numbers])//大于载重,不装
                {
                    d[numbersj] = 0;
                    return 0;
                }
                else
                {
                    d[numbersj] = 1;
                    return profit[numbers];
                }
            }
            if (j < weight[i])
            {
                d[ij] = 0;
                return f(i + 1 j);
            }
            ////根据状态转移方程
            ////f[ij]=max{f[i-1j]f[i-1j-w[i]]+p[i]}
            int p0 = f(i + 1 j);
            int p1 = f(i + 1 j - weight[i]) + profit[i];
            if (p0 < p1)
            {
                d[ij] = 1;
                return p1;
            }
            else
            {
                d[ij] = 0;
                return p0;
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2665  2009-12-03 16:39  WindowsFormsApplication1\WindowsFormsApplication1\BagProblem.cs

     文件     112640  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

     文件      30208  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb

     文件      14328  2009-12-03 16:44  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe

     文件        490  2009-06-11 05:14  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest

     文件       2865  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs

     文件       9112  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs

     文件     155634  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx

     文件       1259  2009-12-03 16:44  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt

     文件        847  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.GenerateResource.Cache

     文件     112640  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe

     文件     100244  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form1.resources

     文件      30208  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.pdb

     文件        180  2009-12-03 09:41  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Properties.Resources.resources

     文件        505  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\Program.cs

     文件       1420  2009-12-03 16:39  WindowsFormsApplication1\WindowsFormsApplication1\Properties\AssemblyInfo.cs

     文件       2898  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Resources.Designer.cs

     文件       5612  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Resources.resx

     文件       1109  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Settings.Designer.cs

     文件        249  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Settings.settings

     文件       3835  2009-12-02 16:47  WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1.csproj

     文件        962  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1.sln

    ..A..H.     24064  2009-12-03 20:15  WindowsFormsApplication1\WindowsFormsApplication1.suo

     目录          0  2009-12-02 10:49  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\Refactor

     目录          0  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug\TempPE

     目录          0  2009-12-03 09:41  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug

     目录          0  2009-12-03 16:40  WindowsFormsApplication1\WindowsFormsApplication1\obj\Debug

     目录          0  2009-12-02 16:39  WindowsFormsApplication1\WindowsFormsApplication1\bin

     目录          0  2009-12-02 10:22  WindowsFormsApplication1\WindowsFormsApplication1\obj

     目录          0  2009-12-03 16:39  WindowsFormsApplication1\WindowsFormsApplication1\Properties

............此处省略5个文件信息

评论

共有 条评论