110 lines
3.4 KiB
C#
110 lines
3.4 KiB
C#
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
|
|
using CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CzokoŚmieciarka.MonoGameView.Algorithms
|
|
{
|
|
public class DFS
|
|
{
|
|
public DFS()
|
|
{
|
|
}
|
|
int count = 0;
|
|
|
|
|
|
public List<IStep> BestPath(AGarbageCollector collector, object[,] grid)
|
|
{
|
|
var r=Search(collector, grid, 0).Key;
|
|
Console.WriteLine(count);
|
|
return r;
|
|
}
|
|
|
|
List<IStep> PossibleSteps(AGarbageCollector collector, object[,] grid)
|
|
{
|
|
|
|
|
|
var result = new List<IStep>();
|
|
var moveSteps = new List<IStep>()
|
|
{
|
|
new MoveStep(Direction.Up),
|
|
new MoveStep(Direction.Down),
|
|
new MoveStep(Direction.Left),
|
|
new MoveStep(Direction.Right)
|
|
};
|
|
var filteredMoveSteps = new List<IStep>();
|
|
foreach (var item in moveSteps)
|
|
{
|
|
var copyCollector = (AGarbageCollector)collector.Clone();
|
|
var copyGrid = (object[,])grid.Clone();
|
|
try
|
|
{
|
|
item.Invoke(copyCollector, grid);
|
|
var gcx = copyCollector.Coords.X;
|
|
var gcy = copyCollector.Coords.Y;
|
|
if (grid[gcx, gcy] is IRoad1 || grid[gcx, gcy] is IGarbageLocalization || grid[gcx, gcy] is ADump)
|
|
{
|
|
result.Add(item);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
KeyValuePair<List<IStep>, int> Search(AGarbageCollector collector, object[,] grid, int length)
|
|
{
|
|
count++;
|
|
if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
|
|
var possibleSteps = PossibleSteps(collector, grid);
|
|
|
|
foreach (var item in possibleSteps)
|
|
{
|
|
var copyCollector = (AGarbageCollector)collector.Clone();
|
|
var copyGrid = (object[,])grid.Clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2();
|
|
item.Invoke(copyCollector, copyGrid);
|
|
var s = Search(copyCollector, copyGrid, length + 1);
|
|
if (s.Key != null)
|
|
{
|
|
s.Key.Insert(0, item);
|
|
return s;
|
|
}
|
|
}
|
|
return new KeyValuePair<List<IStep>, int>(null, length);
|
|
/*var min = int.MaxValue;
|
|
var min_index = 0;
|
|
for (int i = 0; i < mapped.Count; i++)
|
|
{
|
|
if (mapped.ElementAt(i).Value <= min)
|
|
{
|
|
min = mapped.ElementAt(i).Value;
|
|
min_index = i;
|
|
}
|
|
}
|
|
return mapped.ElementAt(min_index);
|
|
*/
|
|
|
|
|
|
}
|
|
}
|
|
}
|