Exemplo n.º 1
0
        //this method return the end location and also return/populates the backtrack array with the counter/step value which will be used later to get the shortest distance
        private MazeCell MoveRobotInBFSManner(int row, int col, int rowlength, int collength, char[,] maze, int[,] backtrack, char endgoal)
        {
            //We create the mazecell
            int              counter   = 0;
            MazeCell         startnode = new MazeCell(row, col, counter);
            Queue <MazeCell> myqueue   = new Queue <MazeCell>();
            MazeCell         parent    = null;
            int              rowno     = 0;
            int              colno     = 0;

            //Look if the given maze is not allowed to edit..then we can use a dictionary of Mazecell and query the mazecell collection to see whether these row and col is visited
            //This can be used if the given maze is not allowed to edit...and we can use visited as the bool in the dictionary
            Dictionary <string, bool> MazeCellVisitTracker = new Dictionary <string, bool>();

            //Mark the start as visited
            MazeCellVisitTracker.Add(startnode.Row + "-" + startnode.Col, true);


            myqueue.Enqueue(startnode);
            while (myqueue.Count != 0)
            {
                parent  = myqueue.Dequeue();
                counter = parent.StepNumber;

                //The robot can move in four direction North, south, east and west
                //north
                rowno = parent.Row - 1;
                colno = parent.Col;

                MazeCell possiblemove = RobotMove(rowno, colno, rowlength, collength, counter, maze, endgoal, MazeCellVisitTracker);
                if (possiblemove != null)
                {
                    //this mean robot has a possible solution or found the goal
                    if (possiblemove.LastCell)
                    {
                        return(possiblemove);
                    }
                    else
                    {
                        //this will be a possible sln move so add it to queue and backtrack
                        myqueue.Enqueue(possiblemove); //this already contains the counter incremented
                        backtrack[possiblemove.Row, possiblemove.Col] = possiblemove.StepNumber;
                    }
                }

                //south
                rowno = parent.Row + 1;
                colno = parent.Col;

                possiblemove = RobotMove(rowno, colno, rowlength, collength, counter, maze, endgoal, MazeCellVisitTracker);
                if (possiblemove != null)
                {
                    //this mean robot has a possible solution or found the goal
                    if (possiblemove.LastCell)
                    {
                        return(possiblemove);
                    }
                    else
                    {
                        //this will be a possible sln move so add it to queue and backtrack
                        myqueue.Enqueue(possiblemove); //this already contains the counter incremented
                        backtrack[possiblemove.Row, possiblemove.Col] = possiblemove.StepNumber;
                    }
                }

                //east
                rowno = parent.Row;
                colno = parent.Col + 1;

                possiblemove = RobotMove(rowno, colno, rowlength, collength, counter, maze, endgoal, MazeCellVisitTracker);
                if (possiblemove != null)
                {
                    //this mean robot has a possible solution or found the goal
                    if (possiblemove.LastCell)
                    {
                        return(possiblemove);
                    }
                    else
                    {
                        //this will be a possible sln move so add it to queue and backtrack
                        myqueue.Enqueue(possiblemove); //this already contains the counter incremented
                        backtrack[possiblemove.Row, possiblemove.Col] = possiblemove.StepNumber;
                    }
                }

                //west
                rowno = parent.Row;
                colno = parent.Col - 1;

                possiblemove = RobotMove(rowno, colno, rowlength, collength, counter, maze, endgoal, MazeCellVisitTracker);
                if (possiblemove != null)
                {
                    //this mean robot has a possible solution or found the goal
                    if (possiblemove.LastCell)
                    {
                        return(possiblemove);
                    }
                    else
                    {
                        //this will be a possible sln move so add it to queue and backtrack
                        myqueue.Enqueue(possiblemove); //this already contains the counter incremented
                        backtrack[possiblemove.Row, possiblemove.Col] = possiblemove.StepNumber;
                    }
                }
            }

            return(null);
        }