Implement a stack class

Assignment Help JAVA Programming
Reference no: EM131373603

Question: 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. 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: EM131373603

Questions Cloud

Explain how concept of an index improve performance : Discuss the concept of an index and explain how they improve performance. Identify and discuss what the most practical and easily applied lesson you learned was. Also, discuss which lesson was the hardest for you to grasp? Why?
What are the implications of multiple generations : What are the implications of multiple generations for managing HSOs/HSs - Which functions of management are most affected by multiple generations in the workforce?
What is required sample size for determining the proportion : What is the required sample size for determining the proportion of defective items in a production process if the proportion is to be known to within 0.05 with 90% confidence? No guess as to the value of the population proportion is available.
Confidence interval for the average transit time : A random sample of 20 shipments gives x‾ = 2.6 days and s = 0.4 day. Give a 99% confidence interval for the average transit time.
Implement a stack class : The backtracking algorithm needs a stack, you should implement a stack class. Each item in the stack is the position of a cell in the maze --- that is, the row and column number of the cell.
Providing care for several patients : 1. A nurse practitioner is providing care for several patients on a medical unit of a hospital. In which of the following patient situations would the nurse practitioner be most likely to rule out hypertension as a contributing factor?
When is it better to allocate an object statically on stack : CPSC 131- In general, when is it better to allocate an object statically on the stack (as opposed to dynamically on the heap)? Give an example of a programming scenario where an object should certainly be stack-allocated.
Construct a confidence interval for average wealth : The following is a random sample of the wealth, in billions of U.S. dollars, of individuals listed on the Forbes "Billionaires" list for 2007.- Construct a 90% confidence interval for the average wealth in $ billions for the people on the Forbes li..
Calculate the expected primary consolidation settlement : Calculate the expected primary consolidation settlement. The results of a consolidation test on the clay are given below.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Task 1university grading system maintains number of tables

task 1university grading system maintains number of tables to store retrieve and manipulate student marks. these tables

  1 ra row your boatprepare a world with a boat a person

1. ra row your boatprepare a world with a boat a person sitting in the boat an island and a pier located 25 meters from

  Write a method that prompts the user for a word and prints

Write a method that prompts the user for a word and prints out its equivalent in Pig Latin. To translate a word to Pig Latin, take the initial letter, move it to the end of the word and add 'ay'. The new suffix (first letter with ay) should be pri..

  Prompts user to enter 7 elements

Write a java application that prompts user to enter 7 elements. The elements will be stored in an array list of type double.  All elements entered should then be displayed on a separate line. The sum of the elements should also be shown in the end li..

  Considered to be an improved version

Part (c) is considered to be an improved version of Part (b). You may use an array (2-dimessional) to store some values that has been computed during the run so that when making recursive calls the program does not compute certain values over and ..

  Create a class called oddintegers with a main method

Create a class called OddIntegers with a main method.  Write the code that will compute the sum of the first n positive odd integers.  For example, if n is 5 you should compute 1 + 3 + 5 + 7 + 9.

  Creation of classes and objects

Introduction of programming using the Java language. The foundation of object-oriented programming will be th topic here. This will include creation of classes and objects, object responsibilities and characteristics, and UML class diagrams

  Write a java method that adds two fractions together

Write a Java method that adds two fractions together

  Implement a simple paddle ball game

Implement a simple paddle ball game. Paddle Ball Game Overview The paddle ball game is a simplification of the Pong game. In the Pong game, a ball is moving around the display, bouncing off walls.

  Create a program in java that displays hello world

Create a program in Java that displays "Hello world!", No Design Section is required for this assignment, Copy and Paste your code into the Source Programs section and a screen shot of the results in the Output section.

  Prepare executable program and a dictionary program

Prepare executable program and a dictionary Program.

  What is localization and why add it to your app

Is Google Cloud Messaging the best approach to use for push notifications? What is localization and why add it to your app?

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