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

  Program obtaining the new scriptdownload and save the

program obtaining the new scriptdownload and save the attached comment cgi mailer script form-mail2.pl to your servers

  Java interfaces and multiple inheritance

In C++, a derived class may have multiple base classes. In contrast, a Java derived class may only have one base class but may implement more than one interface. This question asks you to compare these two language designs.

  Design java program to enter name in input dialog box

Design the program called MailingLabel.java Program must ask user to enter name and address in input dialog box with each component of address seperated by comma.

  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

  Create a class named integerset.

"The fourth array set is named fourthArrayName and was created by calling Method intersectionOfSets on the first two sets. It represents this set of numbers: { value1, value2, value3, valuen }." (Again note: that each number of the array is follow..

  What is the full path the to location of the web application

suppose that you are creating a java web application named "cset-test" consisting of a JSP file named "main.jsp" , and a java Servlet in a file named "InfoServlet.java". The user's home directory is /home/jdoe.

  Program to track hourly employee arrival and departure time

THE JAVA SOURCE CODEA company hires you to write a program to track hourly employee arrival and departure times from work. In essence, you are tasked to make an online time clock. The time clock shall keep a history of an employee’s hours for a two-w..

  System schedule on a multiprocessor-multicore environment

This program assignment is provided to let the students understand how to the Operating Systems schedule all the processes on a multiprocessor or multicore environment.

  Finding java code error

Indicate in the given code segment if an error occurs. If so, point to whether it is syntax, runtime or logic and how you would fix it.

  Video store application

Prepare an object-oriented program in Java for a video store application with the subsequent

  Program create an application that uses a priorityqueue to

create an application that uses a quotpriorityqueuequot to perform the following ltbrgt ltbrgtuses the constructor that

  Trade-offs between quantity and quality of production

Are there inevitable trade-offs between quantity and quality of production and in what ways can job analysis and job redesign help manage there trade-offs?

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