Пример #1
0
        private List <IShowContainer> ContainerPool = new List <IShowContainer>(); //存放所有的集合

        public override void FindWay(ContainerDictionary dict, int x, int y)
        {
            dict[x, y].StartDistance = 0;
            EndMinPool.Add(dict[x, y]);
            while (EndMinPool.Count > 0)
            {
                FindWayType4_(dict);
            }
        }
Пример #2
0
 public override void FindWay(ContainerDictionary dict, int x, int y)
 {
     dict[x, y].StartDistance = 0;
     CurrentList.Add(dict[x, y]);
     while (CurrentList.Count > 0)
     {
         FindWayType2_(dict);
     }
 }
Пример #3
0
        private void FindWayType4_(ContainerDictionary dict)
        {
            foreach (var con in EndMinPool)
            {
                var x = con.X;
                var y = con.Y;
                for (int i = 0; i < 4; i++)
                {
                    var curX = x + Direction.DirectionArray[i].X_;
                    var curY = y + Direction.DirectionArray[i].Y_;
                    //如果是终点
                    if (curX == endX && curY == endY)
                    {
                        wayResult = dict[x, y].Way;
                        EndMinPool.Clear();
                        return;
                    }
                    //如果超出范围
                    if (
                        curX < 0 || curX > dict.TotalX || curY < 0 || curY > dict.TotalY ||//坐标超出范围
                        !dict[curX, curY].CanPass ||//该坐标无法通过
                        dict[curX, curY].IsChecked//该坐标已经检查过了
                        )
                    {
                        continue;
                    }
                    //添加入过程列表
                    wayProcedure.Add(dict[curX, curY]);
                    dict[curX, curY].StartDistance = dict[x, y].StartDistance + 1;//设置距离
                    dict[curX, curY].EndDistance   = Math.Abs(endX - curX) + Math.Abs(endY - curY);
                    dict[curX, curY].IsChecked     = true;

                    dict[curX, curY].Way = Extension.Clone(dict[x, y].Way);//设置当前格子路径,当前格子路径为其前一个格子的路径
                    dict[curX, curY].Way.Add(new Coordinate {
                        X_ = curX, Y_ = curY
                    });                                                               //加上其自身

                    ContainerPool.Add(dict[curX, curY]);
                }
            }
            if (ContainerPool.Count > 0)
            {
                //将所有的池中的EndDistance的元素选出,并将其从池中删除
                var minDistane = ContainerPool.Min(c => c.EndDistance + c.StartDistance);
                EndMinPool = ContainerPool.Where(c => c.EndDistance + c.StartDistance == minDistane).ToList();
                EndMinPool.ForEach(e => ContainerPool.Remove(e));
            }
            else
            {
                EndMinPool.Clear();
            }
        }
Пример #4
0
        private void FindWayType2_(ContainerDictionary dict)
        {
            foreach (var con in CurrentList)
            {
                var x = con.X;
                var y = con.Y;
                for (int i = 0; i < 4; i++)
                {
                    var curX = x + Direction.DirectionArray[i].X_;
                    var curY = y + Direction.DirectionArray[i].Y_;
                    //如果是终点
                    if (curX == endX && curY == endY)
                    {
                        wayResult = dict[x, y].Way;
                        CurrentList.Clear();
                        return;
                    }
                    //如果超出范围
                    if (
                        curX < 0 || curX > dict.TotalX || curY < 0 || curY > dict.TotalY ||//坐标超出范围
                        !dict[curX, curY].CanPass ||//该坐标无法通过
                        dict[curX, curY].IsChecked//该坐标已经检查过了
                        )
                    {
                        continue;
                    }
                    //添加入过程列表
                    wayProcedure.Add(dict[curX, curY]);
                    dict[curX, curY].StartDistance = dict[x, y].StartDistance + 1;//设置距离
                    dict[curX, curY].IsChecked     = true;

                    dict[curX, curY].Way = Extension.Clone(dict[x, y].Way);//设置当前格子路径,当前格子路径为其前一个格子的路径
                    dict[curX, curY].Way.Add(new Coordinate {
                        X_ = curX, Y_ = curY
                    });                                                               //加上其自身

                    NextList.Add(dict[curX, curY]);
                }
            }
            CurrentList = NextList;
            NextList    = new List <IShowContainer>();
        }
Пример #5
0
        public override void FindWay(ContainerDictionary dict, int x, int y)
        {
            var result = new List <IShowContainer>();

            //循环左、上、右、下
            for (int i = 0; i < 4; i++)
            {
                //如果已经出结果了就直接返回
                if (wayResult.Count > 0)
                {
                    return;
                }
                var curX = x + Direction.DirectionArray[i].X_;
                var curY = y + Direction.DirectionArray[i].Y_;
                //如果该坐标是终点
                if (curX == endX && curY == endY)
                {
                    wayResult = dict[x, y].Way;
                    return;
                }

                if (
                    curX < 0 || curX > dict.TotalX || curY < 0 || curY > dict.TotalY ||//坐标超出范围
                    !dict[curX, curY].CanPass ||//该坐标无法通过
                    dict[curX, curY].IsChecked   //该坐标已经检查过了
                    )
                {
                    continue;
                }
                dict[curX, curY].IsChecked = true;     //当前格子已经判断过了
                wayProcedure.Add(dict[curX, curY]);    //添加入过程列表

                dict[curX, curY].Way = dict[x, y].Way; //设置当前格子路径,当前格子路径为其前一个格子的路径
                dict[curX, curY].Way.Add(new Coordinate {
                    X_ = curX, Y_ = curY
                });                                                               //加上其自身
                //递归查找路径
                FindWay(dict, curX, curY);
            }
        }
Пример #6
0
        public string Execute(ContainerDictionary dict)
        {
            var result = string.Empty;

            //判断是否存在起点终点
            if (dict.StartContainer == null)
            {
                return("没有出发点");
            }
            if (dict.EndContainer == null)
            {
                return("没有终点");
            }
            //设定起点终点坐标
            startX = dict.StartContainer.X;
            startY = dict.StartContainer.Y;
            endX   = dict.EndContainer.X;
            endY   = dict.EndContainer.Y;
            //起点的路径为其自身
            dict.StartContainer.Way = new List <Coordinate> {
                new Coordinate {
                    X_ = dict.StartContainer.X, Y_ = dict.StartContainer.Y
                }
            };

            FindWay(dict, startX, startY);
            if (wayResult.Count == 0)
            {
                return("找不到路径");
            }
            var startCoordinate = wayResult.FirstOrDefault(c => c.X_ == dict.StartContainer.X && c.Y_ == dict.StartContainer.Y);
            var endCoordinate   = wayResult.FirstOrDefault(c => c.X_ == dict.EndContainer.X && c.Y_ == dict.EndContainer.Y);

            wayResult.Remove(startCoordinate);
            wayResult.Remove(endCoordinate);
            wayResult = wayResult.Distinct().ToList();
            result    = "成功";
            return(result);
        }
Пример #7
0
 public override void FindWay(ContainerDictionary dict, int x, int y)
 {
 }
Пример #8
0
 public abstract void FindWay(ContainerDictionary dict, int x, int y);