private List <TileDay24Route> CalcRouteMoves(List <string> map, TileDay24Route currentTile, TileDay24Route targetTile) { var possibleTiles = new List <TileDay24Route>() { new TileDay24Route { X = currentTile.X, Y = currentTile.Y - 1, Parent = currentTile, Cost = currentTile.Cost + 1 }, new TileDay24Route { X = currentTile.X, Y = currentTile.Y + 1, Parent = currentTile, Cost = currentTile.Cost + 1 }, new TileDay24Route { X = currentTile.X - 1, Y = currentTile.Y, Parent = currentTile, Cost = currentTile.Cost + 1 }, new TileDay24Route { X = currentTile.X + 1, Y = currentTile.Y, Parent = currentTile, Cost = currentTile.Cost + 1 }, }; possibleTiles.ForEach(tile => tile.SetDistance(targetTile.X, targetTile.Y)); var maxX = map.First().Length - 1; var maxY = map.Count - 1; return(possibleTiles .Where(tile => tile.X >= 0 && tile.X <= maxX) .Where(tile => tile.Y >= 0 && tile.Y <= maxY) .Where(tile => char.IsDigit(map[tile.Y][tile.X]) || map[tile.Y][tile.X] == '.') .ToList()); }
private int CalcRoute(List <string> map, int StartVal, int EndVal) { var start = new TileDay24Route(); start.Y = map.FindIndex(x => x.Contains(StartVal.ToString())); start.X = map[start.Y].IndexOf(StartVal.ToString()); start.Val = StartVal; var finish = new TileDay24Route(); finish.Y = map.FindIndex(x => x.Contains(EndVal.ToString())); finish.X = map[finish.Y].IndexOf(EndVal.ToString()); finish.Val = EndVal; start.SetDistance(finish.X, finish.Y); var activeTiles = new List <TileDay24Route>(); activeTiles.Add(start); var visitedTiles = new List <TileDay24Route>(); while (activeTiles.Any()) { var checkTile = activeTiles.OrderBy(x => x.CostDistance).First(); if (checkTile.X == finish.X && checkTile.Y == finish.Y) { return(checkTile.CostDistance); } visitedTiles.Add(checkTile); activeTiles.Remove(checkTile); var walkableTiles = CalcRouteMoves(map, checkTile, finish); foreach (var walkableTile in walkableTiles) { if (visitedTiles.Any(x => x.X == walkableTile.X && x.Y == walkableTile.Y)) { continue; } if (activeTiles.Any(x => x.X == walkableTile.X && x.Y == walkableTile.Y)) { var existingTile = activeTiles.First(x => x.X == walkableTile.X && x.Y == walkableTile.Y); if (existingTile.CostDistance > checkTile.CostDistance) { activeTiles.Remove(existingTile); activeTiles.Add(walkableTile); } } else { activeTiles.Add(walkableTile); } } } return(0); }