Ejemplo n.º 1
0
Archivo: Maze.cs Proyecto: roycefan/dev
        string SolveMazeXFSInternal(Action <List <NormalNode>, NormalNode> action)
        {
            var Open_Table   = new List <NormalNode>();
            var Closed_Table = new List <NormalNode>();
            var Answer       = new NormalNode();
            var cx           = MapSize.Width - 1;
            var cy           = MapSize.Height - 1;

            var Start = PtStart;
            var End   = PtEnd;

            Debug.Assert(Start != End);

            Open_Table.Add(new NormalNode()
            {
                Coordinate = Start, Parent = null
            });
            NormalNode PathEndNode = null;

            var  Opens   = 0;
            var  Closeds = 0;
            long tick    = 0;

            var watch = new Stopwatch();

            watch.Start();

            while (Open_Table.Count > 0)
            {
                var ExpandNode = Open_Table.First();
                Open_Table.RemoveAt(0);

                if (ExpandNode.Coordinate == End)
                {
                    PathEndNode = ExpandNode;
                    Opens       = Open_Table.Count;
                    Closeds     = Closed_Table.Count;
                    tick        = watch.ElapsedTicks;
                }

                var PtCurrent = ExpandNode.Coordinate;

                var cnt = 0;
                if (PtCurrent.X > 0 && GetDoorState(PtCurrent, DirectionType.Left) == Open && MzSolveMap[PtCurrent.X - 1, PtCurrent.Y] == Closed)
                {
                    action(Open_Table, new NormalNode()
                    {
                        Coordinate = new Point(PtCurrent.X - 1, PtCurrent.Y), Parent = ExpandNode
                    }); MzSolveMap[PtCurrent.X - 1, PtCurrent.Y] = WrongBranch; cnt++;
                }
                if (PtCurrent.Y > 0 && GetDoorState(PtCurrent, DirectionType.Up) == Open && MzSolveMap[PtCurrent.X, PtCurrent.Y - 1] == Closed)
                {
                    action(Open_Table, new NormalNode()
                    {
                        Coordinate = new Point(PtCurrent.X, PtCurrent.Y - 1), Parent = ExpandNode
                    }); MzSolveMap[PtCurrent.X, PtCurrent.Y - 1] = WrongBranch; cnt++;
                }
                if (PtCurrent.X < cx && GetDoorState(PtCurrent, DirectionType.Right) == Open && MzSolveMap[PtCurrent.X + 1, PtCurrent.Y] == Closed)
                {
                    action(Open_Table, new NormalNode()
                    {
                        Coordinate = new Point(PtCurrent.X + 1, PtCurrent.Y), Parent = ExpandNode
                    }); MzSolveMap[PtCurrent.X + 1, PtCurrent.Y] = WrongBranch; cnt++;
                }
                if (PtCurrent.Y < cy && GetDoorState(PtCurrent, DirectionType.Down) == Open && MzSolveMap[PtCurrent.X, PtCurrent.Y + 1] == Closed)
                {
                    action(Open_Table, new NormalNode()
                    {
                        Coordinate = new Point(PtCurrent.X, PtCurrent.Y + 1), Parent = ExpandNode
                    }); MzSolveMap[PtCurrent.X, PtCurrent.Y + 1] = WrongBranch; cnt++;
                }

                watch.Stop();
                if (cnt == 0)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = EndBranch;
                    var it = ExpandNode.Parent as NormalNode;
                    while (it != null)
                    {
                        if (MzSolveMap[it.Coordinate.X, it.Coordinate.Y] == WrongBranch)
                        {
                            it.Branches--;
                            if (it.Branches != 0)
                            {
                                break;
                            }
                        }
                        it = it.Parent as NormalNode;
                    }
                }
                else if (cnt == 1)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = Open;
                }
                else
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = WrongBranch;
                    ExpandNode.Branches = cnt;
                }
                watch.Start();

                Closed_Table.Add(ExpandNode);
            }

            watch.Stop();

            if (PathEndNode == null)
            {
                return("搜索失败,没有最终解");
            }

            MzSolveMap[Start.X, Start.Y] = StartFlag;
            MzSolveMap[End.X, End.Y]     = EndFlag;

            var iter = PathEndNode;

            while (iter != null)
            {
                var pt = iter.Coordinate;
                if (MzSolveMap[pt.X, pt.Y] == WrongBranch)
                {
                    MzSolveMap[pt.X, pt.Y] = RightBranch;
                }
                MzSolvePath.Insert(0, pt);
                iter = iter.Parent as NormalNode;
            }

            return(string.Format(
                       @"步数:{0}
OPEN表总数:{1}
CLOSED表总数:{2}
所耗时钟周期:{3}"
                       , MzSolvePath.Count - 1, Opens, Closeds, tick));
        }
Ejemplo n.º 2
0
Archivo: Maze.cs Proyecto: roycefan/dev
        private string SolveMazeHEU_Internal()
        {
            var Open_Table   = new List <HeuNode>();
            var Closed_Table = new List <HeuNode>();
            var Answer       = new HeuNode();
            var cx           = MapSize.Width - 1;
            var cy           = MapSize.Height - 1;

            var Start = PtStart;
            var End   = PtEnd;

            Debug.Assert(Start != End);

            Open_Table.Add(new HeuNode()
            {
                Coordinate = Start, Parent = null, Level = 0, Cost = End.X + End.Y - Start.X - Start.Y
            });
            NormalNode PathEndNode = null;

            var  Opens   = 0;
            var  Closeds = 0;
            long tick    = 0;

            var watch = new Stopwatch();

            watch.Start();

            while (Open_Table.Count > 0)
            {
                Open_Table.Sort(new Comparison <HeuNode>((a, b) => a.Cost - b.Cost));

                var ExpandNode = Open_Table.First();
                Open_Table.RemoveAt(0);

                var PossibleResult = Open_Table.Where(a => a.Cost == ExpandNode.Cost).ToList().Find(a => a.Coordinate == End);
                if (PossibleResult != null)
                {
                    ExpandNode = PossibleResult;
                }

                if (ExpandNode.Coordinate == End)
                {
                    PathEndNode = ExpandNode;
                    Opens       = Open_Table.Count;
                    Closeds     = Closed_Table.Count;
                    tick        = watch.ElapsedTicks;
                }

                var PtCurrent = ExpandNode.Coordinate;
                var TempList  = new List <HeuNode>();

                if (PtCurrent.X > 0 && GetDoorState(PtCurrent, DirectionType.Left) == Open)
                {
                    TempList.Add(new HeuNode()
                    {
                        Coordinate = new Point(PtCurrent.X - 1, PtCurrent.Y)
                    });
                }
                if (PtCurrent.Y > 0 && GetDoorState(PtCurrent, DirectionType.Up) == Open)
                {
                    TempList.Add(new HeuNode()
                    {
                        Coordinate = new Point(PtCurrent.X, PtCurrent.Y - 1)
                    });
                }
                if (PtCurrent.X < cx && GetDoorState(PtCurrent, DirectionType.Right) == Open)
                {
                    TempList.Add(new HeuNode()
                    {
                        Coordinate = new Point(PtCurrent.X + 1, PtCurrent.Y)
                    });
                }
                if (PtCurrent.Y < cy && GetDoorState(PtCurrent, DirectionType.Down) == Open)
                {
                    TempList.Add(new HeuNode()
                    {
                        Coordinate = new Point(PtCurrent.X, PtCurrent.Y + 1)
                    });
                }

                var cnt = 0;
                TempList.ForEach(a =>
                {
                    a.Parent = ExpandNode;
                    a.Level  = ExpandNode.Level + 1;
                    a.Cost   = a.Level + End.X + End.Y - a.Coordinate.X - a.Coordinate.Y;

                    var open_found   = Open_Table.Find(b => b.Coordinate == a.Coordinate);
                    var closed_found = open_found == null ? Closed_Table.Find(b => b.Coordinate == a.Coordinate) : null;
                    var found        = open_found ?? closed_found;
                    if (found == null)
                    {
                        Open_Table.Add(a);
                        cnt++;
                    }
                    else if (found.Cost > a.Cost)
                    {
                        found.Cost   = a.Cost;
                        found.Parent = ExpandNode;
                        found.Level  = a.Level;
                        if (closed_found != null)
                        {
                            Open_Table.Add(found);
                            Closed_Table.Remove(found);
                        }
                    }
                });

                watch.Stop();
                if (cnt == 0)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = EndBranch;
                    var it = ExpandNode.Parent as NormalNode;
                    while (it != null)
                    {
                        if (MzSolveMap[it.Coordinate.X, it.Coordinate.Y] == WrongBranch)
                        {
                            it.Branches--;
                            if (it.Branches != 0)
                            {
                                break;
                            }
                        }
                        it = it.Parent as NormalNode;
                    }
                }
                else if (cnt == 1)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = Open;
                }
                else
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = WrongBranch;
                    ExpandNode.Branches = cnt;
                }
                watch.Start();

                Closed_Table.Add(ExpandNode);
            }

            watch.Stop();

            if (PathEndNode == null)
            {
                return("搜索失败,没有最终解");
            }

            MzSolveMap[Start.X, Start.Y] = StartFlag;
            MzSolveMap[End.X, End.Y]     = EndFlag;

            var iter = PathEndNode;

            while (iter != null)
            {
                var pt = iter.Coordinate;
                if (MzSolveMap[pt.X, pt.Y] == WrongBranch)
                {
                    MzSolveMap[pt.X, pt.Y] = RightBranch;
                }
                MzSolvePath.Insert(0, pt);
                iter = iter.Parent as NormalNode;
            }

            return(string.Format(
                       @"步数:{0}
OPEN表总数:{1}
CLOSED表总数:{2}
所耗时钟周期:{3}"
                       , MzSolvePath.Count - 1, Opens, Closeds, tick));
        }
Ejemplo n.º 3
0
        string SolveMazeXFSInternal(Action<List<NormalNode>, NormalNode> action)
        {
            var Open_Table = new List<NormalNode>();
            var Closed_Table = new List<NormalNode>();
            var Answer = new NormalNode();
            var cx = MapSize.Width - 1;
            var cy = MapSize.Height - 1;

            var Start = PtStart;
            var End = PtEnd;
            Debug.Assert(Start != End);

            Open_Table.Add(new NormalNode() { Coordinate = Start, Parent = null });
            NormalNode PathEndNode = null;

            var Opens = 0;
            var Closeds = 0;
            long tick = 0;

            var watch = new Stopwatch();
            watch.Start();

            while (Open_Table.Count > 0)
            {
                var ExpandNode = Open_Table.First();
                Open_Table.RemoveAt(0);

                if (ExpandNode.Coordinate == End)
                {
                    PathEndNode = ExpandNode;
                    Opens = Open_Table.Count;
                    Closeds = Closed_Table.Count;
                    tick = watch.ElapsedTicks;
                }

                var PtCurrent = ExpandNode.Coordinate;

                var cnt = 0;
                if (PtCurrent.X > 0 && GetDoorState(PtCurrent, DirectionType.Left) == Open && MzSolveMap[PtCurrent.X - 1, PtCurrent.Y] == Closed)
                {
                    action(Open_Table, new NormalNode() { Coordinate = new Point(PtCurrent.X - 1, PtCurrent.Y), Parent = ExpandNode }); MzSolveMap[PtCurrent.X - 1, PtCurrent.Y] = WrongBranch; cnt++;
                }
                if (PtCurrent.Y > 0 && GetDoorState(PtCurrent, DirectionType.Up) == Open && MzSolveMap[PtCurrent.X, PtCurrent.Y - 1] == Closed)
                {
                    action(Open_Table, new NormalNode() { Coordinate = new Point(PtCurrent.X, PtCurrent.Y - 1), Parent = ExpandNode }); MzSolveMap[PtCurrent.X, PtCurrent.Y - 1] = WrongBranch; cnt++;
                }
                if (PtCurrent.X < cx && GetDoorState(PtCurrent, DirectionType.Right) == Open && MzSolveMap[PtCurrent.X + 1, PtCurrent.Y] == Closed)
                {
                    action(Open_Table, new NormalNode() { Coordinate = new Point(PtCurrent.X + 1, PtCurrent.Y), Parent = ExpandNode }); MzSolveMap[PtCurrent.X + 1, PtCurrent.Y] = WrongBranch; cnt++;
                }
                if (PtCurrent.Y < cy && GetDoorState(PtCurrent, DirectionType.Down) == Open && MzSolveMap[PtCurrent.X, PtCurrent.Y + 1] == Closed)
                {
                    action(Open_Table, new NormalNode() { Coordinate = new Point(PtCurrent.X, PtCurrent.Y + 1), Parent = ExpandNode }); MzSolveMap[PtCurrent.X, PtCurrent.Y + 1] = WrongBranch; cnt++;
                }

                watch.Stop();
                if (cnt == 0)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = EndBranch;
                    var it = ExpandNode.Parent as NormalNode;
                    while (it != null)
                    {
                        if (MzSolveMap[it.Coordinate.X, it.Coordinate.Y] == WrongBranch)
                        {
                            it.Branches--;
                            if (it.Branches != 0)
                                break;
                        }
                        it = it.Parent as NormalNode;
                    }
                }
                else if (cnt == 1)
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = Open;
                }
                else
                {
                    MzSolveMap[PtCurrent.X, PtCurrent.Y] = WrongBranch;
                    ExpandNode.Branches = cnt;
                }
                watch.Start();

                Closed_Table.Add(ExpandNode);
            }

            watch.Stop();

            if (PathEndNode == null)
            {
                return "搜索失败,没有最终解";
            }

            MzSolveMap[Start.X, Start.Y] = StartFlag;
            MzSolveMap[End.X, End.Y] = EndFlag;

            var iter = PathEndNode;
            while (iter != null)
            {
                var pt = iter.Coordinate;
                if (MzSolveMap[pt.X, pt.Y] == WrongBranch)
                    MzSolveMap[pt.X, pt.Y] = RightBranch;
                MzSolvePath.Insert(0, pt);
                iter = iter.Parent as NormalNode;
            }

            return string.Format(
            @"步数:{0}
            OPEN表总数:{1}
            CLOSED表总数:{2}
            所耗时钟周期:{3}"
                , MzSolvePath.Count - 1, Opens, Closeds, tick);
        }