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

  Perform the acyclic-topological sort algorithm

Perform the acyclic-topological sort algorithm on the directed graph having vertex set a-k and edges {(j; a);(j; g);(a; b);(a; e);(b; c);(c; k);(d; e);(e; c);(e; f);(e; i);(f; k); (g; d);(g; e);(g; h);(h; e);(h; i);(i; f);(i; k)} Show the state of th..

  Create algorithm which takes as inputs matrices

Create the algorithm which takes as inputs, matrices C, D, and vertex indices i and j, and returns minimum-cost path from vertex i to vertex j.

  Prepare the initial linked list of students and grades

Write a C program which initially presents menu of choices for the user. Menu must consist of the following choices: Prepare the initial linked list of students and grades.

  What is the difference between syntax and semantics

Explain the distinction between an ambiguity in a proposed algorithm and an ambiguity in the representation of an algorithm.

  Creating visual studio asp .net web site

Make a Visual Studio 2008 ASP .NET Web Site with 2-Web Forms. Add a DropDownList server control and a Label server control to 1st Web Form.

  Separate inventory database

A 20-year old corporation, SewWorld, comprised of 6-locations in three states, sells sewing machines, sewing related software, and accessories. Each store sells between 3-5 different brands of sewing equipments.

  Write the code to implement the method

The "linked list" has a integer "position". In an array, the position is very easy to implement as it is related to the "index" of the array. In the "linked list", the position is much more difficult.

  Explain sorting algorithm which is optimal in cost

Explain a sorting algorithm which is optimal with respect to this cost model and uses O(n) space. That is, time used by algorithm should exactly match lower bound

  Arrays & more loops practice

Write a program ArraysAndLoops and implement the following in the main method:

  What-if and goal-seeking analysis

Problem 1: What-if and Goal-seeking analysis, Problem 2: Portfolio Planning using optimization, Problem 3: A Monte Carlo Simulation Problem

  Describe the need for complex data structures

Describe the need for complex data structures and how they are used. Describe the design and application of arrays and how the array simplifies program development.

  Program for stack by using dynamically allocated array

Write a C++ class which implements stack by using a dynamically allocated array. Initial size of particular stack must be determined when it is created.

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