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

  Algorithm to take input m constraints over n variables

For instance, the constraints x1 = x2, x2 = x3, x3 = x4, and x1 6= x4 cannot be satis fied. Give an efficient algorithm that takes as input m constraints over n variables and decides whether the constraints can be satis fied.

  Create ef?cient algorithm to fnd redundancies

Fnd the redundancies m1, · · · , mn that are within the available budget and that maximize probability that system works correctly. Create an ef?cient algorithm.

  Finding approximation algorithm and ratio of the algorithm

finding approximation algorithm and the ratio of the algoirthm.

  Kind of switching to configure switch to use

Your network's traffic load is very high all times, day and night. What kind of switching do you configure switch to use?

  Adopting agile development methodologies

Relative advantages are the degree to which a new technology is perceived to be superior to current technology. An company is more likely to adopt new technology when it perceives greater relative

  Determining entropy of encrypted message

If this message is encrypted with DES by using a random 56-bit key, determine encrypted message's entropy?

  Determine complete list of nodes which ancestor

Let the following tree: tree a. Determine the children of Q? b. What is the complete list of nodes which have D as ancestor? c. Determine the height of this tree (as height is defined in text)?

  Finding median by black-box worst-case linear-time algorithm

Given a black-box worst-case linear-time algorithm for ?nding median, create algorithm solving the selection problem for an arbitrary order statistic.

  Create algorithm-smallest element-set of combined elements

Assume that X and Y are two sorted sequences, comprising m and n elements respectively. Create the algorithm to nd kth smallest element in set of m + n combined elements.

  Show the postfix expressions

An infix expression is one in which operators are located between their operands - Pop the stack elements and add them to the queue (PostQueue) one by one until the top of the stack has an element of lower precedence

  Design adatabase to keep track of all students at university

Discuss how you would design a database to keep track of all students at a university. Explain tables, Primary Keys, Foreign Keys, relationships, attributes, Candidate Keys.

  How do a bubble sort in mips?

How do a bubble sort in MIPS?

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