public void SetKeyCell(Cell cell) { Cell cellFind = KeyCells.FirstOrDefault(record => record.Location.Equals(cell.Location)); if (cellFind == null) { this.KeyCells.Add(cell); MapBmpFillRectangle(cell, Color.DarkSlateBlue); } else { cellFind.Name = cell.Name; } }
public int GetAverageManhattanDistanceBetweenKeys() { int totalManhattanDistance = 0; int totalPairs = 0; var keyCellList = KeyCells.Select(kvp => kvp.Value).ToList(); for (int i = 0; i < keyCellList.Count; i++) { for (int j = i + 1; j < keyCellList.Count; j++) { totalManhattanDistance += GridPoint.GetManhattanDistance(keyCellList[i], keyCellList[j]); totalPairs++; } } if (totalPairs == 0) { return(0); } return(totalManhattanDistance / totalPairs); }
public bool BeginSearch() { if (Cell.IsSampleCell(this.StartCell, this.GoalCell)) { return(true); } this.StartCell.EvaluateDistance = this.H(StartCell); //将起始节点添加到closedList集合中 this.ClosedList.Add(StartCell); //不停的搜索直到到达目的地 while (true) { if (!this.SearchBestCell()) { //MessageBox.Show("苦海无边,回头是岸!"); return(false); } Cell closedCell = this.ClosedList[ClosedList.Count - 1]; Cell _upCell = new Cell(new Point(closedCell.Location.X, closedCell.Location.Y + CELL_WIDTH)); Cell _downCell = new Cell(new Point(closedCell.Location.X, closedCell.Location.Y - CELL_WIDTH)); Cell _rightCell = new Cell(new Point(closedCell.Location.X + CELL_WIDTH, closedCell.Location.Y)); Cell _leftCell = new Cell(new Point(closedCell.Location.X - CELL_WIDTH, closedCell.Location.Y)); //_upCell.Location = new Point(closedCell.Location.X, closedCell.Location.Y + CELL_WIDTH); //_downCell.Location = new Point(closedCell.Location.X, closedCell.Location.Y - CELL_WIDTH); //_rightCell.Location = new Point(closedCell.Location.X + CELL_WIDTH, closedCell.Location.Y); //_leftCell.Location = new Point(closedCell.Location.X - CELL_WIDTH, closedCell.Location.Y); #region if (Cell.IsSampleCell(_upCell, StartCell)) { closedCell.UpCell = StartCell; } else if (Cell.IsSampleCell(_downCell, StartCell)) { closedCell.DownCell = StartCell; } else if (Cell.IsSampleCell(_rightCell, StartCell)) { closedCell.RightCell = StartCell; } else if (Cell.IsSampleCell(_leftCell, StartCell)) { closedCell.LeftCell = StartCell; } //switch (lastStartSearchWay) //{ // case 0: // if (Cell.IsSampleCell(_upCell, StartCell)) // { // closedCell.UpCell = StartCell; // lastStartSearchWay = 0; // } // else if (Cell.IsSampleCell(_downCell, StartCell)) // { // closedCell.DownCell = StartCell; // lastStartSearchWay = 1; // } // else if (Cell.IsSampleCell(_rightCell, StartCell)) // { // closedCell.RightCell = StartCell; // lastStartSearchWay = 2; // } // else if (Cell.IsSampleCell(_leftCell, StartCell)) // { // closedCell.LeftCell = StartCell; // lastStartSearchWay = 3; // } // break; // case 1: // if (Cell.IsSampleCell(_downCell, StartCell)) // { // closedCell.DownCell = StartCell; // lastStartSearchWay = 1; // } // else if (Cell.IsSampleCell(_rightCell, StartCell)) // { // closedCell.RightCell = StartCell; // lastStartSearchWay = 2; // } // else if (Cell.IsSampleCell(_leftCell, StartCell)) // { // closedCell.LeftCell = StartCell; // lastStartSearchWay = 3; // } // else if (Cell.IsSampleCell(_upCell, StartCell)) // { // closedCell.UpCell = StartCell; // lastStartSearchWay = 0; // } // break; // case 2: // if (Cell.IsSampleCell(_rightCell, StartCell)) // { // lastStartSearchWay = 2; // closedCell.RightCell = StartCell; // } // else if (Cell.IsSampleCell(_leftCell, StartCell)) // { // lastStartSearchWay = 3; // closedCell.LeftCell = StartCell; // } // else if (Cell.IsSampleCell(_upCell, StartCell)) // { // closedCell.UpCell = StartCell; // lastStartSearchWay = 0; // } // else if (Cell.IsSampleCell(_downCell, StartCell)) // { // closedCell.DownCell = StartCell; // lastStartSearchWay = 1; // } // break; // case 3: // if (Cell.IsSampleCell(_leftCell, StartCell)) // { // closedCell.LeftCell = StartCell; // lastStartSearchWay = 3; // } // else if (Cell.IsSampleCell(_upCell, StartCell)) // { // closedCell.UpCell = StartCell; // lastStartSearchWay = 0; // } // else if (Cell.IsSampleCell(_downCell, StartCell)) // { // closedCell.DownCell = StartCell; // lastStartSearchWay = 1; // } // else if (Cell.IsSampleCell(_rightCell, StartCell)) // { // closedCell.RightCell = StartCell; // lastStartSearchWay = 2; // } // break; //} #endregion #region if (Cell.IsSampleCell(_upCell, GoalCell)) { GoalCell.DownCell = closedCell; break; } if (Cell.IsSampleCell(_downCell, GoalCell)) { GoalCell.UpCell = closedCell; break; } if (Cell.IsSampleCell(_rightCell, GoalCell)) { GoalCell.LeftCell = closedCell; break; } if (Cell.IsSampleCell(_leftCell, GoalCell)) { GoalCell.RightCell = closedCell; break; } //if (lastGoalSearchWay == 0) //{ // if (Cell.IsSampleCell(_upCell, GoalCell)) // { // GoalCell.DownCell = closedCell; // lastGoalSearchWay = 0; // break; // } // if (Cell.IsSampleCell(_downCell, GoalCell)) // { // GoalCell.UpCell = closedCell; // lastGoalSearchWay = 1; // break; // } // if (Cell.IsSampleCell(_rightCell, GoalCell)) // { // GoalCell.LeftCell = closedCell; // lastGoalSearchWay = 2; // break; // } // if (Cell.IsSampleCell(_leftCell, GoalCell)) // { // GoalCell.RightCell = closedCell; // lastGoalSearchWay = 3; // break; // } //} //else if (lastGoalSearchWay == 1) //{ // if (Cell.IsSampleCell(_downCell, GoalCell)) // { // GoalCell.UpCell = closedCell; // lastGoalSearchWay = 1; // break; // } // if (Cell.IsSampleCell(_rightCell, GoalCell)) // { // GoalCell.LeftCell = closedCell; // lastGoalSearchWay = 2; // break; // } // if (Cell.IsSampleCell(_leftCell, GoalCell)) // { // GoalCell.RightCell = closedCell; // lastGoalSearchWay = 3; // break; // } // if (Cell.IsSampleCell(_upCell, GoalCell)) // { // GoalCell.DownCell = closedCell; // lastGoalSearchWay = 0; // break; // } //} //else if (lastGoalSearchWay == 2) //{ // if (Cell.IsSampleCell(_rightCell, GoalCell)) // { // GoalCell.LeftCell = closedCell; // lastGoalSearchWay = 2; // break; // } // if (Cell.IsSampleCell(_leftCell, GoalCell)) // { // GoalCell.RightCell = closedCell; // lastGoalSearchWay = 3; // break; // } // if (Cell.IsSampleCell(_upCell, GoalCell)) // { // GoalCell.DownCell = closedCell; // lastGoalSearchWay = 0; // break; // } // if (Cell.IsSampleCell(_downCell, GoalCell)) // { // GoalCell.UpCell = closedCell; // lastGoalSearchWay = 1; // break; // } //} //else if (lastGoalSearchWay == 3) //{ // if (Cell.IsSampleCell(_leftCell, GoalCell)) // { // GoalCell.RightCell = closedCell; // lastGoalSearchWay = 3; // break; // } // if (Cell.IsSampleCell(_upCell, GoalCell)) // { // GoalCell.DownCell = closedCell; // lastGoalSearchWay = 0; // break; // } // if (Cell.IsSampleCell(_downCell, GoalCell)) // { // GoalCell.UpCell = closedCell; // lastGoalSearchWay = 1; // break; // } // if (Cell.IsSampleCell(_rightCell, GoalCell)) // { // GoalCell.LeftCell = closedCell; // lastGoalSearchWay = 2; // break; // } //} #endregion } List <Cell> bestLoad = new List <Cell>(); Cell lastClosedCell = new Cell(); float n = float.MaxValue; foreach (Cell closedCell in this.ClosedList) { if (closedCell.EvaluateDistance <= n) { n = closedCell.EvaluateDistance; } } foreach (Cell closedCell in this.ClosedList) { if (Math.Abs(closedCell.EvaluateDistance - n) < 0.00001) { lastClosedCell = closedCell; break; } } while (true) { if (Cell.IsSampleCell(lastClosedCell.ParentCell, StartCell)) { bestLoad.Add(lastClosedCell); break; } else { bestLoad.Add(lastClosedCell); lastClosedCell = lastClosedCell.ParentCell; } } for (int i = 0; i < bestLoad.Count; i++) { if (i == 0) { GoalCell.ParentCell = bestLoad[i]; } else { bestLoad[i - 1].ParentCell = bestLoad[i]; } if (i < bestLoad.Count - 1) { Cell _upCell = new Cell(); Cell _downCell = new Cell(); Cell _rightCell = new Cell(); Cell _leftCell = new Cell(); _upCell.Location = new Point(bestLoad[i].Location.X, bestLoad[i].Location.Y + CELL_WIDTH); _downCell.Location = new Point(bestLoad[i].Location.X, bestLoad[i].Location.Y - CELL_WIDTH); _rightCell.Location = new Point(bestLoad[i].Location.X + CELL_WIDTH, bestLoad[i].Location.Y); _leftCell.Location = new Point(bestLoad[i].Location.X - CELL_WIDTH, bestLoad[i].Location.Y); if (Cell.IsSampleCell(_upCell, bestLoad[i + 1])) { bestLoad[i].UpCell = bestLoad[i + 1]; } else if (Cell.IsSampleCell(_downCell, bestLoad[i + 1])) { bestLoad[i].DownCell = bestLoad[i + 1]; } else if (Cell.IsSampleCell(_rightCell, bestLoad[i + 1])) { bestLoad[i].RightCell = bestLoad[i + 1]; } else if (Cell.IsSampleCell(_leftCell, bestLoad[i + 1])) { bestLoad[i].LeftCell = bestLoad[i + 1]; } } } MapBmpFillRectangles(bestLoad, Color.Blue); PathPoint = new List <Point> { GoalCell.Location }; Cell activeCheckCell = GoalCell; do { if (KeyCells.FindIndex(record => record.Location.Equals(activeCheckCell.Location)) > -1) { activeCheckCell = activeCheckCell.ParentCell; PathPoint.Add(activeCheckCell.Location); continue; } if (activeCheckCell.LeftCell != null) { if (activeCheckCell.ParentCell.LeftCell != null) { activeCheckCell = activeCheckCell.ParentCell; continue; } activeCheckCell = activeCheckCell.ParentCell; //if (PathPoint.Count > 2) //{ // Point p1 = PathPoint[PathPoint.Count - 2]; // Point p2 = PathPoint[PathPoint.Count - 1]; // if ((Math.Abs( p1.X - p2.X) <= CELL_WIDTH // &&Math.Abs( p1.Y -p2.Y )<= CELL_WIDTH) // && (Math.Abs(activeCheckCell.Location.X - p2.X) <= CELL_WIDTH // && Math.Abs(activeCheckCell.Location.Y - p2.Y) <= CELL_WIDTH) // ) // { // PathPoint.RemoveAt(PathPoint.Count - 1); // } //} PathPoint.Add(activeCheckCell.Location); continue; } else if (activeCheckCell.RightCell != null) { if (activeCheckCell.ParentCell.RightCell != null) { activeCheckCell = activeCheckCell.ParentCell; continue; } activeCheckCell = activeCheckCell.ParentCell; //if (PathPoint.Count > 2) //{ // Point p1 = PathPoint[PathPoint.Count - 2]; // Point p2 = PathPoint[PathPoint.Count - 1]; // if ((Math.Abs(p1.X - p2.X) <= CELL_WIDTH // && Math.Abs(p1.Y - p2.Y) <= CELL_WIDTH) // && (Math.Abs(activeCheckCell.Location.X - p2.X) <= CELL_WIDTH // && Math.Abs(activeCheckCell.Location.Y - p2.Y) <= CELL_WIDTH) // ) // { // PathPoint.RemoveAt(PathPoint.Count - 1); // } //} PathPoint.Add(activeCheckCell.Location); continue; } else if (activeCheckCell.UpCell != null) { if (activeCheckCell.ParentCell.UpCell != null) { activeCheckCell = activeCheckCell.ParentCell; continue; } activeCheckCell = activeCheckCell.ParentCell; //if (PathPoint.Count > 2) //{ // Point p1 = PathPoint[PathPoint.Count - 2]; // Point p2 = PathPoint[PathPoint.Count - 1]; // if ((Math.Abs(p1.X - p2.X) <= CELL_WIDTH // && Math.Abs(p1.Y - p2.Y) <= CELL_WIDTH) // && (Math.Abs(activeCheckCell.Location.X - p2.X) <= CELL_WIDTH // && Math.Abs(activeCheckCell.Location.Y - p2.Y) <= CELL_WIDTH) // ) // { // PathPoint.RemoveAt(PathPoint.Count - 1); // } //} PathPoint.Add(activeCheckCell.Location); continue; } else if (activeCheckCell.DownCell != null) { if (activeCheckCell.ParentCell.DownCell != null) { activeCheckCell = activeCheckCell.ParentCell; continue; } activeCheckCell = activeCheckCell.ParentCell; //if (PathPoint.Count > 2) //{ // Point p1 = PathPoint[PathPoint.Count - 2]; // Point p2 = PathPoint[PathPoint.Count - 1]; // if ((Math.Abs(p1.X - p2.X) <= CELL_WIDTH // && Math.Abs(p1.Y - p2.Y) <= CELL_WIDTH) // && (Math.Abs(activeCheckCell.Location.X - p2.X) <= CELL_WIDTH // && Math.Abs(activeCheckCell.Location.Y - p2.Y) <= CELL_WIDTH) // ) // { // PathPoint.RemoveAt(PathPoint.Count - 1); // } //} PathPoint.Add(activeCheckCell.Location); continue; } } while (activeCheckCell != null && activeCheckCell.ParentCell != null); return(true); }
public void RemoveKeyCell(Cell cell) { KeyCells.Remove(cell); MapBmpFillRectangle(cell, Color.Azure); }
public Cell GetKeyCell(int mouseX, int mouseY) { return(KeyCells.FirstOrDefault(record => record.Location.Equals(new Point(mouseX / this.CELL_WIDTH * CELL_WIDTH, mouseY / this.CELL_WIDTH * CELL_WIDTH)))); }
public string GetKeyCellName(int mouseX, int mouseY) { Cell cell = KeyCells.FirstOrDefault(record => record.Location.Equals(new Point(mouseX / this.CELL_WIDTH * CELL_WIDTH, mouseY / this.CELL_WIDTH * CELL_WIDTH))); return(cell == null ? string.Empty : cell.Name); }