C# 分支定界法 01背包问题用C#编程通过分支定界法解决背包问题.急.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/22 02:35:18
C# 分支定界法 01背包问题用C#编程通过分支定界法解决背包问题.急.
C# 分支定界法 01背包问题
用C#编程通过分支定界法解决背包问题.急.
C# 分支定界法 01背包问题用C#编程通过分支定界法解决背包问题.急.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test01Bag
{
public partial class Form1 :Form
{
public Form1()
{
InitializeComponent();
}
public class Product
{
#region 商品类
///
/// 商品重量泛型List存储
///
private List weight = new List();
public List Weight
{
get{ return weight; }
set{ weight = value; }
}
private List value = new List();
///
/// 商品价值泛型List存储
///
public List Value
{
get { return this.value; }
set{ this.value = value; }
}
private int count;
///
/// 商品数量
///
public int Count
{
get
{
count = weight.Count;
return count;
}
}
///
/// 添加商品信息
///
/// 重量
/// 价值
///
public int setWeightAddValve(int w,int v)
{
weight.Add(w);
value.Add(v);
return weight.Count;
}
#endregion
}
public class Bag
{
#region 背包类
int[,] help = new int[100,100];
int[] weight = new int[100];
int[] value = new int[100];
int count;
int temp;
// 背包容量
private int valume;
public int Valume
{
get{ return valume; }
set { valume = value; }
}
private int maxvalue;
public int Maxvalue
{
get { return maxvalue; }
set{ maxvalue = value; }
}
///
/// 设定容量temp暂存
///
///
public void setValume(int v)
{
valume = v;
temp = valume;
}
///
/// 引入数据
///
/// Product
public void setProduct(Product newprd)
{
count = newprd.Count;
weight = newprd.Weight.ToArray();
value = newprd.Value.ToArray();
}
///
/// 买商品
///
///
public int buyproduct()
{
//初始化help表第一行为零
for (int w = 0; w < valume; w++)
{
help[0,w] = 0;
}
for (int i = 1; i