Using a backtracking algorithm

Assignment Help Data Structure & Algorithms
Reference no: EM13316104

In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the exit or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it reaches a position from which there is an untried path. The backtracking algorithm always tries all directions from any position, and always in the same order.

The input to the algorithm is a maze with walls (represented by '1' characters) and open passage ways (represented by '0' characters). The starting position of the mouse is represented by 'm' and the exit from the maze by 'e'. Your program should read the maze in from a file, and the name of the file should be a command-line argument. The first line of the input will contain the number of rows and the number of columns in a maze. Thus, the input might look like the following:

6 5
1 1 1 1 1
1 0 0 e 1
1 1 1 0 1
1 m 1 0 1
1 0 0 0 1
1 1 1 1 1
     
The maze will always have a wall around the outside, so you need not be concerned about the mouse falling off the maze as it explores all directions.  The backtracking algorithm keeps a stack of positions that are the beginnings of paths it has yet to try. From the current position, the algorithm pushes onto the stack any untried open neighboring positions (if there are any), always looking forward, backward, left and right from the current position. At each step, the algorithm pops the top position off the stack and pushes the untried neighboring positions onto the stack. The algorithm must mark each visited position with a period to avoid revisiting positions --- so that it will not
loop forever trying the same routes.

The backtracking algorithm works as follows:

read in the maze;

initialize the stack;

goalCell = the position of the exit in the maze;

startCell = the initial position of the mouse in the maze;

currentCell = startCell;

while currentCell is not the goalCell

  mark currentCell as visited;

push onto the stack the unvisited open neighbours of currentCell;

  if the stack is empty

    the mouse is trapped: we tried all routes and failed to find the

exit;

else

pop a cell from the stack and make it currentCell;

end while;

the mouse can escape the maze: we reached the goal cell.
    
Because the backtracking algorithm needs a stack, you should implement a stack class (or you can use that from COSC 2006). Each item in the stack is the position of a cell in the maze --- that is, the row and column number of the cell.  Your program should print out the maze after each cell is visited, showing which cells have already been visited. Finally, your program must print out a message indicating whether the exit was found or that no route to the exit could be found (the mouse is trapped).

To develop your program, you can use the input file testMaze.txt, which contains the above maze.

Reference no: EM13316104

Questions Cloud

What is the opportunity cost of taking this trip : You plan a major adventure trip for the summer. You won’t be able to take your usual summer job that pays $6,000, and you won’t be able to live at home for free. The cost of your travel
How do you think the higher demand has affected : How do you think the higher demand has affected the equilibrium wage? In which direction do you think the labor supply and demand shifted? Explain your reasoning.
What input current and voltage are needed : A step-up transformer has 24 turns on the primary coil and 780 turns on the secondary coil. what input current and voltage are needed
Which piece of information is more important in the utility : Which piece of information is more important in the utility maximization process: marginal utility per unit of the good or marginal utility per dollar? WHY?
Using a backtracking algorithm : If the backtracking algorithm finds a dead end, it retraces its path until it reaches a position from which there is an untried path. The backtracking algorithm always tries all directions from any position, and always in the same order.
Should it raise prices or lower prices explain : Suppose a pizza parlor is interested in increasing total revenue by adjusting its price. Should it raise prices or lower prices? Explain giving concrete examples.
What could be the main motives for marriott : Identify several major categories of segmentation used by Marriott. For each relate specific examples of hotel services tailored to various target n www.marriott.com offers a brief description of 13 brands of various Marriott hotels catering differ..
Find how long does it take for the grinding wheel to stop : An electric motor rotating a workshop grinding wheel at a rate of 9.80x10^1 rev/min is switched off. how long does it take for the grinding wheel to stop
A demand and supply curve for tickets to the celine dion : Suppose Blanca would prefer a certain income of $20,000 to the expected value of the gamble. Explain her preference toward risk by drawing a graph.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Using command line options in bash shell script

Design a script that will permit the user to enter one of several choices from the command line. The specific requirements are as follows:

  Question about arrays, vector and stl

Suppose if x denotes the mean of a sequence of numbers x1, x2,.....xn, variance is the average of the squares of the deviations of numbers from the mean.

  Research and implement the sieve of eratosthenes

Research and implement the Sieve of Eratosthenes (also called prime sieve) algorithm. Researching and implementing algorithms is something I did frequently while consulting and any programmer must be able to do this

  Give a recursive algorithm for finding the number of one''s

Give a recursive algorithm for finding the number of one's in a bit string, name the algorothm count-ones.

  Create algorithm to read file of employee records

Create the algorithm which will read a file of employee records and produce the weekly report of gross earnings for those employees.

  Creating an exception class and applet file

Create an applet document that prompts the user for an ID number and an age. Construct an Exception class and throw an Exception of that class if the ID is not in the range of valid ID numbers.

  Create algorithm to read arbitrary number of data record

Create the algorithm to read arbitrary number of data records, each containing name, age, and code. Code of 1 will indicate female, a code of 2 will indicate male.

  Design a circular double linked list

Design a circular double linked list, for which the following operations should be implemented

  Explain the three types of relationships

Provide an example of a one to one relationship and an example of a many-to-many relationship in a newspaper, magazine, book, or everyday situation you encounter.

  Describe a fair coin algorithm to returns either 0 or 1

Describe a FAIRCOIN algorithm that returns either 0 or 1 with equal probability, using ONEINTHREE as your only source of randomness.

  C program that accepts a month and day

Make a C program that accepts a month and day for example, June 14 from the keyboard as input. Store this data in a string called date.

  Different applications of data structure

What are the different applications of Data Structure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd