This commit is contained in:
Lendaia Mirai
2025-11-08 16:36:51 +01:00
parent a4592d8349
commit 06af9a6078
2 changed files with 133 additions and 68 deletions

View File

@@ -1,4 +1,69 @@
namespace OE.ALGA.Optimalizalas
using System;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
namespace OE.ALGA.Optimalizalas
{
// 8. heti labor feladat - Tesztek: 08_DinamikusProgramozasTesztek.cs
public class DinamikusHatizsakPakolas
{
HatizsakProblema problema;
int[,] tablazat;
int lepesszam;
public int LepesSzam
{
get { return lepesszam; }
}
public DinamikusHatizsakPakolas(HatizsakProblema problema)
{
this.problema = problema;
}
public int[,] TablazatFeltoltes()
{
int[,] F = new int[problema.N + 1, problema.Wmax + 1];
for (int i = 0; i < problema.N; i++)
{
for (int j = 0; j < problema.Wmax; j++)
{
if (i == 0 || j == 0)
{
F[i, j] = 0;
}
else if (j < problema.W[i])
{
F[i, j] = F[i - 1, j];
}
else
{
int[] tomb = new int[] { (F[i - 1, j]), (F[i - 1, j - problema.W[i]] + Convert.ToInt32(problema.P[i])) };
F[i, j] = tomb.Max();
}
lepesszam++;
}
}
return F;
}
public float OptimalisErtek()
{
tablazat = TablazatFeltoltes();
return tablazat[problema.N, problema.Wmax];
}
public bool[] OptimalisMegoldas()
{
float optimalisertek = OptimalisErtek();
int t = problema.N;
int h = problema.Wmax;
bool[] optimails = new bool[problema.N];
while (t > 0 && h > 0)
{
if (tablazat[t, h] != tablazat[t - 1, h])
{
optimails[t] = true;
h = h - problema.W[t];
}
t--;
}
return optimails;
}
}
}