示例#1
0
 public bool IsInBestLoad(Cell cell, List <Cell> bestLoad)
 {
     foreach (Cell cell1 in bestLoad)
     {
         if (Cell.IsSampleCell(cell, cell1))
         {
             return(true);
         }
     }
     return(false);
 }
示例#2
0
 public bool IsInOpen(Cell cell)
 {
     foreach (Cell cell1 in openList)
     {
         if (Cell.IsSampleCell(cell, cell1))
         {
             return(true);
         }
     }
     return(false);
 }
示例#3
0
 public bool IsInClosed(Cell cell)
 {
     foreach (Cell cell1 in closedList)
     {
         if (Cell.IsSampleCell(cell, cell1))
         {
             return(true);
         }
     }
     return(false);
 }
示例#4
0
 public bool IsStoneOrOutWord(List <Cell> cellList, Cell cell1)
 {
     if (cell1.Location.X >= 0 && cell1.Location.X < this.panelMap.Width && cell1.Location.Y >= 0 && cell1.Location.Y < this.panelMap.Height)
     {
         for (int i = 0; i < cellList.Count; i++)
         {
             if (Cell.IsSampleCell(cell1, cellList[i]))
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
示例#5
0
        public void BeginSearch()
        {
            //开始搜索的时候,计时开始
            stopWatch.Start();
            //不停的搜索直到到达目的地
            while (true)
            {
                this.SearchBestCell();
                if (isState)
                {
                    return;
                }
                Cell closedCell = this.closedList[closedList.Count - 1];
                Cell _upCell    = new Cell();
                Cell _downCell  = new Cell();
                Cell _rightCell = new Cell();
                Cell _leftCell  = new Cell();

                Cell _upLeftCell    = new Cell();
                Cell _downLeftCell  = new Cell();
                Cell _upRightCell   = new Cell();
                Cell _downRightCell = new Cell();

                _upCell.Location    = new Point(closedCell.Location.X, closedCell.Location.Y + 20);
                _downCell.Location  = new Point(closedCell.Location.X, closedCell.Location.Y - 20);
                _rightCell.Location = new Point(closedCell.Location.X + 20, closedCell.Location.Y);
                _leftCell.Location  = new Point(closedCell.Location.X - 20, closedCell.Location.Y);

                if (isUseGlobal)
                {
                    _upLeftCell.Location    = new Point(closedCell.Location.X - 20, closedCell.Location.Y - 20);
                    _downLeftCell.Location  = new Point(closedCell.Location.X - 20, closedCell.Location.Y + 20);
                    _upRightCell.Location   = new Point(closedCell.Location.X + 20, closedCell.Location.Y - 20);
                    _downRightCell.Location = new Point(closedCell.Location.X + 20, closedCell.Location.Y + 20);
                }

                if (Cell.IsSampleCell(_upCell, goalCell))
                {
                    break;
                }
                if (Cell.IsSampleCell(_downCell, goalCell))
                {
                    break;
                }
                if (Cell.IsSampleCell(_rightCell, goalCell))
                {
                    break;
                }
                if (Cell.IsSampleCell(_leftCell, goalCell))
                {
                    break;
                }
                if (isUseGlobal)
                {
                    if (Cell.IsSampleCell(_upLeftCell, goalCell))
                    {
                        break;
                    }
                    if (Cell.IsSampleCell(_downLeftCell, goalCell))
                    {
                        break;
                    }
                    if (Cell.IsSampleCell(_upRightCell, goalCell))
                    {
                        break;
                    }
                    if (Cell.IsSampleCell(_downRightCell, goalCell))
                    {
                        break;
                    }
                }
            }

            List <Cell> bestLoad       = new List <Cell>();
            Cell        lastClosedCell = new Cell();
            int         n = 1000;

            for (int i = 0; i < this.closedList.Count; i++)
            {
                if (this.closedList[i].EvaluateDistance <= n)
                {
                    n = this.closedList[i].EvaluateDistance;
                }
            }

            for (int i = 0; i < this.closedList.Count; i++)
            {
                if (this.closedList[i].EvaluateDistance == n)
                {
                    lastClosedCell = this.closedList[i];
                    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 (isUseGlobal == true)
                {
                    if (bestLoad[0].Location.Y > goalCell.Location.Y && bestLoad[0].Location.X == goalCell.Location.X)
                    {
                        this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Purple), bestLoad[0].Location.X + 1, bestLoad[0].Location.Y + 1 - 20, 19, 19);
                    }
                    if (bestLoad[0].Location.Y < goalCell.Location.Y && bestLoad[0].Location.X == goalCell.Location.X)
                    {
                        this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Purple), bestLoad[0].Location.X + 1, bestLoad[0].Location.Y + 1 + 20, 19, 19);
                    }
                    if (bestLoad[0].Location.X > goalCell.Location.X && bestLoad[0].Location.Y == goalCell.Location.Y)
                    {
                        this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Purple), bestLoad[0].Location.X + 1 - 20, bestLoad[0].Location.Y + 1, 19, 19);
                    }
                    if (bestLoad[0].Location.X < goalCell.Location.X && bestLoad[0].Location.Y == goalCell.Location.Y)
                    {
                        this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Purple), bestLoad[0].Location.X + 1 + 20, bestLoad[0].Location.Y + 1, 19, 19);
                    }
                }
                this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Purple), bestLoad[i].Location.X + 1, bestLoad[i].Location.Y + 1, 19, 19);
                //Thread.Sleep(100);
            }
            this.btnRest.Enabled = true;
            //结束计时
            stopWatch.Stop();
            //转换格式
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = String.Format("{0:00}.{1:00000000}s", ts.Seconds, ts.Milliseconds);

            MessageBox.Show("RunTime: " + elapsedTime);
        }
示例#6
0
        public void SearchBestCell()
        {
            //为起始节点初始化上下左右方格
            upCell    = new Cell();
            downCell  = new Cell();
            rightCell = new Cell();
            leftCell  = new Cell();

            upLeftCell    = new Cell();
            downLeftCell  = new Cell();
            upRightCell   = new Cell();
            downRightCell = new Cell();

            upCell.Location    = new Point(closedList[closedList.Count - 1].Location.X, closedList[closedList.Count - 1].Location.Y - 20);
            downCell.Location  = new Point(closedList[closedList.Count - 1].Location.X, closedList[closedList.Count - 1].Location.Y + 20);
            rightCell.Location = new Point(closedList[closedList.Count - 1].Location.X + 20, closedList[closedList.Count - 1].Location.Y);
            leftCell.Location  = new Point(closedList[closedList.Count - 1].Location.X - 20, closedList[closedList.Count - 1].Location.Y);

            if (isUseGlobal)
            {
                upLeftCell.Location    = new Point(closedList[closedList.Count - 1].Location.X - 20, closedList[closedList.Count - 1].Location.Y - 20);
                downLeftCell.Location  = new Point(closedList[closedList.Count - 1].Location.X - 20, closedList[closedList.Count - 1].Location.Y + 20);
                upRightCell.Location   = new Point(closedList[closedList.Count - 1].Location.X + 20, closedList[closedList.Count - 1].Location.Y - 20);
                downRightCell.Location = new Point(closedList[closedList.Count - 1].Location.X + 20, closedList[closedList.Count - 1].Location.Y + 20);
            }

            //判断这四个方格是否处于边界是否是障碍物,如果不是则将方格加入到Open集合列表中
            if (this.IsStoneOrOutWord(stoneCells, upCell) && !IsInClosed(upCell) && !IsInOpen(upCell))
            {
                upCell.ParentCell       = closedList[closedList.Count - 1];
                upCell.RealDistance     = upCell.ParentCell.RealDistance + 10;
                upCell.EvaluateDistance = this.h(upCell);
                upCell.FinalDistance    = upCell.EvaluateDistance + upCell.RealDistance;
                this.openList.Add(upCell);
                //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), upCell.Location);
                //Thread.Sleep(100);
            }
            if (this.IsStoneOrOutWord(stoneCells, downCell) && !IsInClosed(downCell) && !IsInOpen(downCell))
            {
                downCell.ParentCell       = closedList[closedList.Count - 1];
                downCell.RealDistance     = downCell.ParentCell.RealDistance + 10;
                downCell.EvaluateDistance = this.h(downCell);
                downCell.FinalDistance    = downCell.EvaluateDistance + downCell.RealDistance;
                this.openList.Add(downCell);
                //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), downCell.Location);
                //Thread.Sleep(100);
            }
            if (this.IsStoneOrOutWord(stoneCells, rightCell) && !IsInClosed(rightCell) && !IsInOpen(rightCell))
            {
                rightCell.ParentCell       = closedList[closedList.Count - 1];
                rightCell.RealDistance     = rightCell.ParentCell.RealDistance + 10;
                rightCell.EvaluateDistance = this.h(rightCell);
                rightCell.FinalDistance    = rightCell.EvaluateDistance + rightCell.RealDistance;
                this.openList.Add(rightCell);
                //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), rightCell.Location);
                //Thread.Sleep(100);
            }
            if (this.IsStoneOrOutWord(stoneCells, leftCell) && !IsInClosed(leftCell) && !IsInOpen(leftCell))
            {
                leftCell.ParentCell       = closedList[closedList.Count - 1];
                leftCell.RealDistance     = leftCell.ParentCell.RealDistance + 10;
                leftCell.EvaluateDistance = this.h(leftCell);
                leftCell.FinalDistance    = leftCell.EvaluateDistance + leftCell.RealDistance;
                this.openList.Add(leftCell);
                //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), leftCell.Location);
                //Thread.Sleep(100);
            }
            if (isUseGlobal)
            {
                if (this.IsStoneOrOutWord(stoneCells, upLeftCell) && !IsInClosed(upLeftCell) && !IsInOpen(upLeftCell))
                {
                    upLeftCell.ParentCell       = closedList[closedList.Count - 1];
                    upLeftCell.RealDistance     = upLeftCell.ParentCell.RealDistance + 14;
                    upLeftCell.EvaluateDistance = this.h(upLeftCell);
                    upLeftCell.FinalDistance    = upLeftCell.EvaluateDistance + upLeftCell.RealDistance;
                    this.openList.Add(upLeftCell);
                    //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), upLeftCell.Location);
                    //Thread.Sleep(100);
                }
                if (this.IsStoneOrOutWord(stoneCells, downLeftCell) && !IsInClosed(downLeftCell) && !IsInOpen(downLeftCell))
                {
                    downLeftCell.ParentCell       = closedList[closedList.Count - 1];
                    downLeftCell.RealDistance     = downLeftCell.ParentCell.RealDistance + 14;
                    downLeftCell.EvaluateDistance = this.h(downLeftCell);
                    downLeftCell.FinalDistance    = downLeftCell.EvaluateDistance + downLeftCell.RealDistance;
                    this.openList.Add(downLeftCell);
                    //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), downLeftCell.Location);
                    //Thread.Sleep(100);
                }
                if (this.IsStoneOrOutWord(stoneCells, upRightCell) && !IsInClosed(upRightCell) && !IsInOpen(upRightCell))
                {
                    upRightCell.ParentCell       = closedList[closedList.Count - 1];
                    upRightCell.RealDistance     = upRightCell.ParentCell.RealDistance + 14;
                    upRightCell.EvaluateDistance = this.h(upRightCell);
                    upRightCell.FinalDistance    = upRightCell.EvaluateDistance + upRightCell.RealDistance;
                    this.openList.Add(upRightCell);
                    //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), upRightCell.Location);
                    //Thread.Sleep(100);
                }
                if (this.IsStoneOrOutWord(stoneCells, downRightCell) && !IsInClosed(downRightCell) && !IsInOpen(downRightCell))
                {
                    downRightCell.ParentCell       = closedList[closedList.Count - 1];
                    downRightCell.RealDistance     = downRightCell.ParentCell.RealDistance + 14;
                    downRightCell.EvaluateDistance = this.h(downRightCell);
                    downRightCell.FinalDistance    = downRightCell.EvaluateDistance + downRightCell.RealDistance;
                    this.openList.Add(downRightCell);
                    //this.panelMap.CreateGraphics().DrawString("开", new Font("宋体", 10), new SolidBrush(Color.Red), downRightCell.Location);
                    //Thread.Sleep(100);
                }
            }

            List <Cell> minCells    = new List <Cell>();
            List <Cell> minestCells = new List <Cell>();
            int         min         = 1000;
            int         n           = 1000;

            //在open集合中找出最小的f(n)值
            foreach (Cell cell in openList)
            {
                if (cell.FinalDistance <= min)
                {
                    min = cell.FinalDistance;
                }
            }
            //找出f(n)值最小的格子
            for (int i = 0; i < this.openList.Count; i++)
            {
                if (openList[i].FinalDistance == min)
                {
                    minCells.Add(openList[i]);
                }
            }
            //在f(n)值最小的格子集合中找出最小的g(n)值
            for (int i = 0; i < minCells.Count; i++)
            {
                if (minCells[i].EvaluateDistance <= n)
                {
                    n = minCells[i].EvaluateDistance;
                }
            }
            //在f(n)值最小的格子集合中找出g(n)值最小的格子
            for (int i = 0; i < minCells.Count; i++)
            {
                if (minCells[i].EvaluateDistance == n)
                {
                    minestCells.Add(minCells[i]);
                }
            }
            Random rd = new Random();

            //在f(n)值和g(n)的格子集合中随机选取一个格子
            if (minestCells.Count == 0)
            {
                MessageBox.Show("苦海无边,回头是岸!");
                isState = true;
                return;
            }
            int j = rd.Next(minestCells.Count);

            minestCells[j].CellColor = Color.Yellow;
            this.closedList.Add(minestCells[j]);
            this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Orange), this.closedList[closedList.Count - 1].Location.X + 1, this.closedList[closedList.Count - 1].Location.Y + 1, 19, 19);
            //this.panelMap.CreateGraphics().DrawString("关", new Font("宋体", 10), new SolidBrush(Color.Red), this.closedList[closedList.Count - 1].Location);
            //Thread.Sleep(100);
            this.panelMap.CreateGraphics().FillRectangle(new SolidBrush(Color.Yellow), this.closedList[closedList.Count - 1].Location.X + 1, this.closedList[closedList.Count - 1].Location.Y + 1, 19, 19);
            //从open集合中移除刚刚添加到closed集合中的格子
            for (int i = 0; i < openList.Count; i++)
            {
                if (Cell.IsSampleCell(openList[i], minestCells[j]))
                {
                    openList.Remove(openList[i]);
                    break;
                }
            }
        }