コード例 #1
0
 public BFS()
 {
     Base           = new RouteTree();
     Base.x         = SearchBases.Source[0];
     Base.y         = SearchBases.Source[1];
     _queue         = new Queue <RouteTree>();
     NodeVisited    = 0;
     queueOperation = 0;
 }
コード例 #2
0
ファイル: DFS.cs プロジェクト: MMovasaghi/Search-Algorithms
 public DFS()
 {
     x              = SearchBases.Source[0];
     y              = SearchBases.Source[1];
     Base           = new RouteTree();
     Base.x         = x;
     Base.y         = y;
     _stack         = new Stack <RouteTree>();
     stackOperation = 0;
     NodeVisited    = 0;
 }
コード例 #3
0
        public void ShowWay(RouteTree Node)
        {
            SearchBases.map[Node.x][Node.y] = 'o';
            Console.SetCursorPosition(Node.y, Node.x);
            Console.Write("o");

            while (Node.PrevNode.PrevNode != null)
            {
                NodeVisited++;
                Node = Node.PrevNode;
                SearchBases.map[Node.x][Node.y] = 'o';
                Console.SetCursorPosition(Node.y, Node.x);
                Console.Write("o");
                queueOperation++;
            }
        }
コード例 #4
0
ファイル: DFS.cs プロジェクト: MMovasaghi/Search-Algorithms
        public int FindWay()
        {
            RouteTree Node     = Base;
            bool      Isfinish = false;
            bool      Notfound = false;

            int i = 0;

            while (!AlgorithmBase.ProblemSolved(x, y) && !Isfinish && !Notfound)
            {
                NodeVisited += 4;
                if (SearchBases.map[x - 1][y] == ' ' && Node.Top == null) //top
                {
                    x -= 1;
                    SearchBases.map[x][y] = '.';
                    Console.SetCursorPosition(y, x);
                    Console.Write(".");


                    _stack.Push(Node);
                    Node.Top          = new RouteTree();
                    Node.IsOK         = Route.top;
                    Node.Top.PrevNode = Node;
                    Node      = Node.Top;
                    Node.Down = Node.PrevNode;

                    Node.x = x;
                    Node.y = y;
                }
                else if (SearchBases.map[x][y + 1] == ' ' && Node.Right == null) //right
                {
                    y += 1;
                    SearchBases.map[x][y] = '.';
                    Console.SetCursorPosition(y, x);
                    Console.Write(".");

                    _stack.Push(Node);
                    Node.Right          = new RouteTree();
                    Node.IsOK           = Route.right;
                    Node.Right.PrevNode = Node;
                    Node      = Node.Right;
                    Node.Left = Node.PrevNode;

                    Node.x = x;
                    Node.y = y;
                }
                else if (SearchBases.map[x + 1][y] == ' ' && Node.Down == null) //down
                {
                    x += 1;
                    SearchBases.map[x][y] = '.';
                    Console.SetCursorPosition(y, x);
                    Console.Write(".");

                    _stack.Push(Node);
                    Node.Down          = new RouteTree();
                    Node.IsOK          = Route.down;
                    Node.Down.PrevNode = Node;
                    Node     = Node.Down;
                    Node.Top = Node.PrevNode;

                    Node.x = x;
                    Node.y = y;
                }
                else if (SearchBases.map[x][y - 1] == ' ' && Node.Left == null) //left
                {
                    y -= 1;
                    SearchBases.map[x][y] = '.';
                    Console.SetCursorPosition(y, x);
                    Console.Write(".");

                    _stack.Push(Node);
                    Node.Left          = new RouteTree();
                    Node.IsOK          = Route.left;
                    Node.Left.PrevNode = Node;
                    Node       = Node.Left;
                    Node.Right = Node.PrevNode;

                    Node.x = x;
                    Node.y = y;
                }
                else
                {
                    if (_stack.Count >= 1)
                    {
                        if (_stack.Peek() != null)
                        {
                            Node = _stack.Pop();
                            x    = Node.x;
                            y    = Node.y;
                            stackOperation++;
                        }
                        else
                        {
                            Isfinish = true;
                        }
                    }
                    else
                    {
                        Notfound = true;
                    }
                }
                Thread.Sleep(20);
                i++;
            }
            if (!Notfound)
            {
                _stack.Push(Node);
                stackOperation++;
                ShowWay();
            }
            else
            {
                Console.SetCursorPosition(0, 22);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\t\t\tWAY NOT FOUND");
            }
            return(i);
        }
コード例 #5
0
        public int FindWay()
        {
            RouteTree Node = Base;

            _queue.Enqueue(Node);

            int  i        = 0;
            bool Isfinish = false;

            while (!AlgorithmBase.ProblemSolved(Node.x, Node.y) && _queue.Count >= 0 && !Isfinish)
            {
                NodeVisited += 4;
                if (SearchBases.map[Node.x - 1][Node.y] == ' ' && Node.Top == null) //top
                {
                    Node.Top = new RouteTree()
                    {
                        PrevNode = Node,
                        x        = Node.x - 1,
                        y        = Node.y
                    };

                    SearchBases.map[Node.Top.x][Node.Top.y] = '.';
                    Console.SetCursorPosition(Node.Top.y, Node.Top.x);
                    Console.Write(".");

                    _queue.Enqueue(Node.Top);
                }
                if (SearchBases.map[Node.x][Node.y + 1] == ' ' && Node.Right == null) //right
                {
                    Node.Right = new RouteTree()
                    {
                        PrevNode = Node,
                        x        = Node.x,
                        y        = Node.y + 1
                    };

                    SearchBases.map[Node.Right.x][Node.Right.y] = '.';
                    Console.SetCursorPosition(Node.Right.y, Node.Right.x);
                    Console.Write(".");

                    _queue.Enqueue(Node.Right);
                }
                if (SearchBases.map[Node.x + 1][Node.y] == ' ' && Node.Down == null) //down
                {
                    Node.Down = new RouteTree()
                    {
                        PrevNode = Node,
                        x        = Node.x + 1,
                        y        = Node.y
                    };

                    SearchBases.map[Node.Down.x][Node.Down.y] = '.';
                    Console.SetCursorPosition(Node.Down.y, Node.Down.x);
                    Console.Write(".");

                    _queue.Enqueue(Node.Down);
                }
                if (SearchBases.map[Node.x][Node.y - 1] == ' ' && Node.Left == null) //left
                {
                    Node.Left = new RouteTree()
                    {
                        PrevNode = Node,
                        x        = Node.x,
                        y        = Node.y - 1
                    };

                    SearchBases.map[Node.Left.x][Node.Left.y] = '.';
                    Console.SetCursorPosition(Node.Left.y, Node.Left.x);
                    Console.Write(".");

                    _queue.Enqueue(Node.Left);
                }
                if (_queue.Count > 0)
                {
                    Node = _queue.Dequeue();
                }
                else
                {
                    Isfinish = true;
                }
                queueOperation++;
                Thread.Sleep(20);
                i++;
            }
            if (AlgorithmBase.ProblemSolved(Node.x, Node.y))
            {
                this.ShowWay(Node);
            }
            else
            {
                Console.SetCursorPosition(0, 22);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\t\t\tWAY NOT FOUND");
            }
            return(i);
        }