public void AddNeighbours(CellDistance cellDistance, List <CellDistance> queue, int[][] grid, DistanceForAllBuilding[][] result) { Visit(cellDistance.Building, cellDistance.Row + 1, cellDistance.Col, cellDistance.Distance + 1, queue, grid, result); Visit(cellDistance.Building, cellDistance.Row - 1, cellDistance.Col, cellDistance.Distance + 1, queue, grid, result); Visit(cellDistance.Building, cellDistance.Row, cellDistance.Col + 1, cellDistance.Distance + 1, queue, grid, result); Visit(cellDistance.Building, cellDistance.Row, cellDistance.Col - 1, cellDistance.Distance + 1, queue, grid, result); }
public void Visit(int building, int row, int col, int distance, List <CellDistance> queue, int[][] grid, DistanceForAllBuilding[][] result) { if (row < 0 || row >= grid.Length || col < 0 || col >= grid[0].Length || grid[row][col] != 0) { return; } var currentDistanceFromAllBuildings = result[row][col]; currentDistanceFromAllBuildings.GetDistance(building); if (distance < currentDistanceFromAllBuildings.GetDistance(building)) { currentDistanceFromAllBuildings.AssignDistance(building, distance); var cellDistance = new CellDistance(row, col, distance, building); queue.Add(cellDistance); } }
private static Stack <Cell> calculateRoute(Cell from, Cell to, Mover mover, int distance) { Dictionary <Cell, int> cellToPos = new Dictionary <Cell, int>(); IPriorityQueueHandle <CellDistance> handle = null; IPriorityQueue <CellDistance> abierta = new IntervalHeap <CellDistance>(); Dictionary <Cell, bool> cerrada = new Dictionary <Cell, bool>(); Dictionary <Cell, float> f = new Dictionary <Cell, float>(); Dictionary <Cell, float> g = new Dictionary <Cell, float>(); Dictionary <Cell, Cell> anterior = new Dictionary <Cell, Cell>(); Dictionary <Cell, IPriorityQueueHandle <CellDistance> > handles = new Dictionary <Cell, IPriorityQueueHandle <CellDistance> >(); Cell posInicial = from; g[posInicial] = 0; f[posInicial] = estimaMeta(from, to); anterior[posInicial] = null; abierta.Add(ref handle, new CellDistance(posInicial, f[posInicial])); handles.Add(posInicial, handle); List <Cell> ends = GetSurroundCellsAtRadius(to, distance); while (!abierta.IsEmpty) { CellDistance candidata = abierta.DeleteMin(); Cell celdaCandidata = candidata.cell; handles.Remove(celdaCandidata); if (ends.Contains(celdaCandidata)) { Stack <Cell> ruta = new Stack <Cell>(); reconstruyeCamino(ruta, celdaCandidata, anterior); return(ruta); } Cell[] accesibles = getCeldasAccesibles(celdaCandidata, mover); cerrada[celdaCandidata] = true; foreach (Cell accesible in accesibles) { float posibleG = g[celdaCandidata] + estimaAvance(celdaCandidata, accesible); float posibleF = posibleG + estimaMeta(accesible, to); if (cerrada.ContainsKey(accesible) && cerrada[accesible] && posibleF >= f[accesible]) { continue; } if (!handles.ContainsKey(accesible) || posibleF < f[accesible]) { anterior[accesible] = celdaCandidata; g[accesible] = posibleG; f[accesible] = posibleF; // Modifica inserta si no esta dentro var cd = new CellDistance(accesible, f[accesible]); if (handles.ContainsKey(accesible)) { abierta[handles[accesible]] = cd; } else { handle = null; abierta.Add(ref handle, cd); handles.Add(accesible, handle); } } } } return(null); }