//private int BruteAlgorithm(CellViewModel source, CellViewModel destination) //{ // if (destination.IsObstacle) return -1; // Stack<CellViewModel> path = new Stack<CellViewModel>(); // source.isVisited = true; // int xDifference = destination.XIndex - source.XIndex;//if > 0 go down, else go up // int yDifference = destination.YIndex - source.YIndex;//if > 0 go right else go left // CellViewModel currentCell = source; // path.Push(currentCell); // while(currentCell != destination) // { // if(xDifference > 0) // { // if(Cells.FirstOrDefault(i => i.)) // } // } // return -1; //} private void DrawPath(CellViewModel destination, CellViewModel source) { CellViewModel currentCell = destination; for (int i = destination.Distance; i > 0; i--) { for (int k = 0; k < 4; k++) { int row = currentCell.XIndex + RowNumber[k]; int column = currentCell.YIndex + ColumnNumber[k]; CellViewModel newCell = Cells.FirstOrDefault(j => j.XIndex == row && j.YIndex == column); if (newCell == source) { return; } if (IsValid(row, column) && newCell.Distance != 0) { if (newCell.Distance == (i - 1)) { newCell.Colour = new SolidColorBrush(Colors.Orange); currentCell = newCell; } } } } }
private int BFS(CellViewModel source, CellViewModel destination) { if (destination.IsObstacle) { return(-1); } List <CellViewModel> path = new List <CellViewModel>(); source.isVisited = true; path.Add(source); while (path.Any()) { CellViewModel currentCell = path.FirstOrDefault(); if (currentCell == destination) { return(currentCell.Distance); } path.Remove(currentCell); for (int i = 0; i < 4; i++) { int row = currentCell.XIndex + RowNumber[i]; int column = currentCell.YIndex + ColumnNumber[i]; CellViewModel newCell = Cells.FirstOrDefault(j => j.XIndex == row && j.YIndex == column); if (IsValid(row, column) && !newCell.IsObstacle && !newCell.isVisited) { newCell.isVisited = true; newCell.Distance = currentCell.Distance + 1; if (newCell != Destination) { newCell.Colour = new SolidColorBrush(Colors.LightGreen); Number++; } path.Add(newCell); } } } return(-1); }