Czoko_Smieciarka/Trunk/MonoGameView/Algorithms/DFS.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2019-04-22 14:38:11 +02:00
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;
2019-04-22 14:32:18 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2019-04-22 14:38:11 +02:00
using Microsoft.Xna.Framework.Content;
2019-04-22 14:32:18 +02:00
2019-04-22 14:38:11 +02:00
namespace CzokoŚmieciarka.MonoGameView.Algorithms
2019-04-22 14:32:18 +02:00
{
public class DFS
{
public DFS()
{
}
int count = 0;
2019-04-22 14:38:11 +02:00
public List<IStep> BestPath(ContentManager content, AGarbageCollector collector, object[,] grid)
2019-04-22 14:32:18 +02:00
{
2019-04-22 14:38:11 +02:00
var r=Search(content, collector, grid, 0).Key;
2019-04-22 14:32:18 +02:00
Console.WriteLine(count);
2019-04-22 14:38:11 +02:00
if (r == null) return new List<IStep>();
2019-04-22 14:32:18 +02:00
return r;
}
2019-04-22 14:38:11 +02:00
List<IStep> PossibleSteps(ContentManager content, AGarbageCollector collector, object[,] grid)
2019-04-22 14:32:18 +02:00
{
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)
{
2019-04-22 14:38:11 +02:00
var copyCollector = (AGarbageCollector)collector.Clone(content);
2019-04-22 14:32:18 +02:00
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;
}
2019-04-22 14:38:11 +02:00
KeyValuePair<List<IStep>, int> Search(ContentManager content, AGarbageCollector collector, object[,] grid, int length)
2019-04-22 14:32:18 +02:00
{
count++;
if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
2019-04-22 14:38:11 +02:00
var possibleSteps = PossibleSteps(content, collector, grid);
2019-04-22 14:32:18 +02:00
foreach (var item in possibleSteps)
{
2019-04-22 14:38:11 +02:00
var copyCollector = (AGarbageCollector)collector.Clone(content);
2019-04-22 14:32:18 +02:00
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);
2019-04-22 14:38:11 +02:00
var s = Search(content, copyCollector, copyGrid, length + 1);
2019-04-22 14:32:18 +02:00
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);
*/
}
}
}