Write a program that searches through a maze to find a path from the beginning to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a program that searches through a maze to find a path from the beginning to the end. The maze will be a two-dimensional grid of locations, numbered by row and column. We could draw out a maze like this: 0 1 2 3 0 X X X X 1 E X 2 X S A maze is described by 3 things: the valid locations (places where it's okay to walk, indicated as an X in the figure above), the start location (indicated by S), and the end location (indicated by E). Your program should first read the number of valid locations in the maze, and the row and column of each valid location. It should then read the starting location and the ending location. For the maze pictured above, the input would be: 8 0 0 01 02 03 10 12 20 22 22 10 Your program should be iterative, not recursive. We will use a stack to simulate recursion. The stack forms a natural data structure for storing what has been visited in the past, and the stack makes it easy to go back to a previous decision and try a different location. This type of search is common in compute science, and is called depth-first search (DFS). The driver will contain the logic to solve the puzzle. It should start at the starting location, and is only allowed to visit locations that are valid (according to the input). At each stage, the solver should either move forward in some direction (right, down, left, or up) and push that new location on the stack, or it should back up one location by popping the current location off the stack. This continues until the solve has either reached the end location or it has become empty (indicating that there is no path from the start to the end). The stack should keep track of the locations that have been visited from the start until the current location (the current location should be the one on the top of the stack). Each step should look at the current location and use that location to produce a "neighbor" location. A neighbor location is a location (possibly invalid) which is one step away from the original location. For example, 22 is a neighbor of 1 2 If the neighbor is both a valid location, and is not currently on the path that has been traversed (on the stack), then it will be added to the stack for further exploration. If all the neighbors of the current location (on the top of the stack) have been visited, then that location is removed from the stack (this is called backtracking). The Location class contains both the coordinates of the location and the functionality for an iterator. A Location object is able to iterate over all of its neighbor locations. Please read the comments in the Location.java file for more details of how the iteration should work. Because the stack will contain Location objects, and each Location object is an iterator over its own neighbors, each Location object of the stack will know which of its neighbors is next. Input specification Input will consist of a positive integer, which is the number of valid locations. This will be followed by that many Locations, which form the list of valid Locations. After that will come the start and the end locations. The start location and the end location are guaranteed to be valid. Output specification If no solution to the maze exists, then the program prints: No solution found Otherwise, the program prints a solution in the following format: Solution found: 22 12 02 0 1 0 0 10 Note that the program should report only the first solution it finds, not every solution there may be. The solution the program finds is heavily dependent upon whether the Location class iterates properly over its neighbors. Write a program that searches through a maze to find a path from the beginning to the end. The maze will be a two-dimensional grid of locations, numbered by row and column. We could draw out a maze like this: 0 1 2 3 0 X X X X 1 E X 2 X S A maze is described by 3 things: the valid locations (places where it's okay to walk, indicated as an X in the figure above), the start location (indicated by S), and the end location (indicated by E). Your program should first read the number of valid locations in the maze, and the row and column of each valid location. It should then read the starting location and the ending location. For the maze pictured above, the input would be: 8 0 0 01 02 03 10 12 20 22 22 10 Your program should be iterative, not recursive. We will use a stack to simulate recursion. The stack forms a natural data structure for storing what has been visited in the past, and the stack makes it easy to go back to a previous decision and try a different location. This type of search is common in compute science, and is called depth-first search (DFS). The driver will contain the logic to solve the puzzle. It should start at the starting location, and is only allowed to visit locations that are valid (according to the input). At each stage, the solver should either move forward in some direction (right, down, left, or up) and push that new location on the stack, or it should back up one location by popping the current location off the stack. This continues until the solve has either reached the end location or it has become empty (indicating that there is no path from the start to the end). The stack should keep track of the locations that have been visited from the start until the current location (the current location should be the one on the top of the stack). Each step should look at the current location and use that location to produce a "neighbor" location. A neighbor location is a location (possibly invalid) which is one step away from the original location. For example, 22 is a neighbor of 1 2 If the neighbor is both a valid location, and is not currently on the path that has been traversed (on the stack), then it will be added to the stack for further exploration. If all the neighbors of the current location (on the top of the stack) have been visited, then that location is removed from the stack (this is called backtracking). The Location class contains both the coordinates of the location and the functionality for an iterator. A Location object is able to iterate over all of its neighbor locations. Please read the comments in the Location.java file for more details of how the iteration should work. Because the stack will contain Location objects, and each Location object is an iterator over its own neighbors, each Location object of the stack will know which of its neighbors is next. Input specification Input will consist of a positive integer, which is the number of valid locations. This will be followed by that many Locations, which form the list of valid Locations. After that will come the start and the end locations. The start location and the end location are guaranteed to be valid. Output specification If no solution to the maze exists, then the program prints: No solution found Otherwise, the program prints a solution in the following format: Solution found: 22 12 02 0 1 0 0 10 Note that the program should report only the first solution it finds, not every solution there may be. The solution the program finds is heavily dependent upon whether the Location class iterates properly over its neighbors.
Expert Answer:
Answer rating: 100% (QA)
import javautilScanner import javautilStack class Homework Help static class Location int i j Locationint i int j thisi i thisj j static final int STA... View the full answer
Related Book For
Project Management The Managerial Process
ISBN: 9781260570434
8th Edition
Authors: Eric W Larson, Clifford F. Gray
Posted Date:
Students also viewed these programming questions
-
In this project, we will write a program that controls a quiz show, much like the many popular TV shows. The program will read in a group of questions and their multiple choice answers, storing them...
-
The following is a list of items that could be included in the intangible assets section of the balance sheet. (a) Indicate which items on the list below would generally be reported as intangible...
-
Determine each of the following as being either true or false. If an angle has a cosine of 0.2, then the secant of the angle is 5.
-
Thomson's Model of the Atom Continued. Using Thomson's (outdated) model of the atom described in Problem 22.52, consider an atom consisting of two electrons, each of charge -e, embedded in a sphere...
-
A five-year fixed-rate loan of $100 million carries a 7 percent annual interest rate. The borrower is rated BB. Based on hypothetical historical data, the probability distribution given below has...
-
What are the different types of consulting and litigation support activities for fraud and forensic accounting professionals?
-
Garcia Home Improvement Company installs replacement siding, windows, and louvered glass doors for single-family homes and condominium complexes in northern New Jersey and southern New York. The...
-
If Bob faces a 20% chance of losing $20 or an 80% chance of losing nothing. What is the expected loss and variance?
-
TV Trends to Watch in 2021 In 2021, expect larger and cheaper 4K TVs, a new level of voice control, interaction with other smart products, and mini Led backlights to improve color contrast and...
-
Draw a 20 to 30 year business cycle model for a particular country (Y-axix GDP, X-axis years up to 2022), mark and label the following points on the graph (A. recession, B. depression, C. peak, D....
-
1. David Willis is considering opening up a new copy center store near a large university. If he does, he will rent six machines for $1,200 per month for each machine. Rent, utilities, and wages will...
-
Instruction to be followed. > There are 10 problems/questions: >> you have one attempt per question >> 1 is a general journal problem >>> a text entry general journal is provided >>> there are enough...
-
A solid right angle is a solid angle of steradians (i.e. it defines an area of on the appropriate unit sphere). Show that there exist many distinct solid right angles.
-
A hollow ball with a diameter of 3.81 cm has an average density of 0.0838 g/cm. What force must be applied to hold it stationary while it is completely submerged under water? (Enter the magnitude in...
-
You will be a freshman lawmaker in the U.S. House of Representatives trying to serve your constituents and the long-term goals of the United States. You will need to learn about the federal budget...
-
Bristo Corporation has sales of 1,750 units at $40 per unit. Variable expenses are 30% of the selling price. If total fixed expenses are $39,000, the degree of operating leverage is?
-
What are the four types of poultry production systems? Explain each type.
-
Assume you are the project manager for the Tidal 2 software project. You have been asked to calculate the expected cost for the project. Your companys database indicates hat developers can handle...
-
1 . Why would IBM want to move from computer hardware to service products? 2 . What impact will artificial intelligence (AI) have on the field of project management?
-
What do you think would have happened if for some reason it took Samsung three years to release its next-generation smartphone?
-
Consider the data file \(m r o z\) on working wives. Use the 428 observations on married women who participate in the labor force. In this exercise, we examine the effectiveness of a parent's college...
-
Consider the data file \(m r o z\) on working wives. Use the 428 observations on married women who participate in the labor force. In this exercise, we examine the effectiveness of a parent's college...
-
The CAPM says that the risk premium on security \(j\) is related to the risk premium on the market portfolio. That is where \(r_{j}\) and \(r_{f}\) are the returns to security \(j\) and the risk-free...
Study smarter with the SolutionInn App