Exemplo n.º 1
0
        /// <summary>
        /// 读取最短路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="start">开始</param>
        /// <param name="end">结束</param>
        public void GetShortest(ref List <string> path, MazePointer start, MazePointer end)
        {
            if (end.X == start.X && end.Y == start.Y)
            {
                path.Add(MazeGraph[end.X, end.Y]);
                return;
            }
            var endPointer = VisitedList.FirstOrDefault(x => x.X == end.X && x.Y == end.Y);

            path.Add(MazeGraph[endPointer.X, endPointer.Y]);
            GetShortest(ref path, start, endPointer.Father);
        }
Exemplo n.º 2
0
        public static void Run()
        {
            Console.WriteLine("Test data:");
            Run("data/testdata19.txt");
            Visited.Clear();
            VisitedList.Clear();

            Console.WriteLine("Answer data:");
            Run("data/data19.txt");
            Visited.Clear();
            VisitedList.Clear();
            Console.WriteLine("Answer data B:");
            Run("data/data19b.txt");
        }
Exemplo n.º 3
0
        public void BFS(int node)
        {
            var queue = new Queue <int>();

            queue.Enqueue(node);
            VisitedList.Add(node);
            while (queue.Peek() > 0)
            {
                var pop = queue.Dequeue();
                foreach (var neighbor in AdjacencyList[node])
                {
                    queue.Enqueue(neighbor);
                }
            }
        }
Exemplo n.º 4
0
        private void Init(int gridSize, int polarRegionSize)
        {
            Ensure <ArgumentException>(
                0 < gridSize &&
                0 < polarRegionSize && polarRegionSize < 90);

            this.gridSize        = gridSize;
            this.polarRegionSize = polarRegionSize;

            int latCount = (int)(Math.Ceiling(180.0 - 2.0 * polarRegionSize) / gridSize);
            int lonCount = (int)(Math.Ceiling(360.0 / gridSize));

            content = new List <T> [latCount, lonCount];
            InitContent();

            visited = new VisitedList(content.GetLength(0), content.GetLength(1));
        }
Exemplo n.º 5
0
        public void AStar(MazePointer start, MazePointer end)
        {
            MazePointer initPointer(int x, int y)
            {
                return(new MazePointer()
                {
                    X = x,
                    Y = y,
                    Father = start,
                    W = GetW(end.X - x, end.Y - y),
                    H = start.H + 1
                });
            }

            if (start == null || start.Equals(end))
            {
                return;
            }
            VisitedList.Add(start);
            BackUpList.Remove(start);
            //找到strat周围的点
            MazePointer current = null, top = null, bottom = null, left = null, right = null;

            if (start.Y - 1 >= 0)
            {
                if (MazeGraph[start.X, start.Y - 1] != null)
                {
                    top = initPointer(start.X, start.Y - 1);
                    if (VisitedList.Exists(x => x.X == top.X && x.Y == top.Y))
                    {
                        top = null;
                    }
                    if (top != null)
                    {
                        BackUpList.Add(top);
                    }
                    current = top;
                }
            }
            if (start.Y + 1 < MazeGraph.GetLength(1))
            {
                if (MazeGraph[start.X, start.Y + 1] != null)
                {
                    bottom = initPointer(start.X, start.Y + 1);
                    if (VisitedList.Exists(x => x.X == bottom.X && x.Y == bottom.Y))
                    {
                        bottom = null;
                    }
                    if (bottom != null)
                    {
                        BackUpList.Add(bottom);
                    }
                    current = GetShorter(current, bottom);
                }
            }
            if (start.X - 1 >= 0)
            {
                if (MazeGraph[start.X - 1, start.Y] != null)
                {
                    left = initPointer(start.X - 1, start.Y);
                    if (VisitedList.Exists(x => x.X == left.X && x.Y == left.Y))
                    {
                        left = null;
                    }
                    if (left != null)
                    {
                        BackUpList.Add(left);
                    }
                    current = GetShorter(current, left);
                }
            }
            if (start.X + 1 < MazeGraph.GetLength(0))
            {
                if (MazeGraph[start.X + 1, start.Y] != null)
                {
                    right = initPointer(start.X + 1, start.Y);
                    if (VisitedList.Exists(x => x.X == right.X && x.Y == right.Y))
                    {
                        right = null;
                    }
                    if (right != null)
                    {
                        BackUpList.Add(right);
                    }
                    current = GetShorter(current, right);
                }
            }
            foreach (var backup in BackUpList)
            {
                current = GetShorter(current, backup);
            }
            AStar(current, end);
        }