mirror of
https://github.com/Lendaia/oe-alga-feladatok.git
synced 2026-04-23 12:26:07 +01:00
cigi
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user