Answered step by step
Verified Expert Solution
Question
1 Approved Answer
g Walk Wuugil d e , yUU WIII PIUUJUIy Walk UUWII d paull as lal as you Il you dle all n can go. Eventually,
g Walk Wuugil d e , yUU WIII PIUUJUIy Walk UUWII d paull as lal as you Il you dle all n can go. Eventually, you will reach your destination or you won't be able to go any farther. If you can't go any farther, you will need to consider alternative paths. Therefore we need to be able to systematically perform trial and error search. Backtracking is a way for doing just this. It is a systematic, nonrepetitive approach to trying alternative paths and eliminating them if they don't work. Recursion allows you to implement backtracking in a relatively straightforward manner. Each activation frame is used to remember the choice that was made at that particular decision point. After returning from a recursive call you can perform other recursive calls to try out different paths. In this lab assignment, you will be implementing a recursive program to find a path through a maze that employs this backtrack search. The maze itself consists of a two-dimensional grid of colored cells. The maze will start at the cell (0,0) and the exit point is the cell (getNCols()-1.getNRows()-1). The operations get NCols and getNRows() are methods of the class TwoDimGrid. All cells that can be part of Il be in the NON_BACKGROUND color. All the cells that represent barriers and cannot be part of the path will be in the BACKGROUND color. To keep track of a cell that we have visited, we will set it to the TEMPORARY color. If we find a path, all cells on the path will be reset to the PATH color. So there is a total of four possible colors. Initially, the maze has all cells set to color BACKGROUND, as depicted in Fig. 1. The values we will use for each of these constants are as follows and they are defined in Maze class. Green for PATH White for BACKGROUND Red for NON_BACKGROUND Black for TEMPORARY You will use the method getColor() of Maze class to get the color of a cell and its recolor() method to set the color of a cell. You will be completing the recursive method findMazePath() in Maze.java. Maze Test class implements the visual grid and path through the maze using AWT. To test your program you will be running the Maze Test class. When you run the main() method of this class, it will prompt you for the size of the grid, i.e. number of rows and columns, it will display an initial grid as in Fig. 1(a) where all the cells have BACKGROUND color You can edit the grid by pressing on a cell to change its color to NON BACKGROUND (red) as in 1(b). In 1(b), we can move from cell (0,0) to (0,1) but to (1,0); from cell (1,2) we can move left, right and down but not up. When you press solve button, it will call findMazePath you had implememented which will find a path from (0,0) to (4,4) by coloring the cells in the path with color PATH(i.e. green). Fig. 1(a) Fig. 1(b) Fib. 1(c) Toggle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 4,0 0,1 1,1 2,1 3,1 4,1 0, 21, 22, 23,2 0,2 down but not up. When you press solve button, it will call findMazePath you had implememented which will find a path from (0,0) to (4,4) by coloring the cells in the path with color PATH (i.e. green). Fig. 1(a) Fig. 1(b) Fib. 1(c) Toggle a button to change its color Press SOLVE when ready Toogle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 3.0 3,0 0,1 0, 11, 12,1 1, 2 0,3 0,4 1,4 2,4 SOLVE RESET SOLVE RESET SOLVE RESET Here is an example of a maze which does not have a path from (0,0) to (4,4). Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2.0 SOLVE RESET When you nin the program you should set the following where cells colored TEMPORARY Toggle a button to change its color Press SOLVE when ready 0,1 1,1 0,2 1,2 0,3 SOLVE RESET When you run the program, you should get the following where cells colored TEMPORARY (Black) have been visited. Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 3,0 Message 0, 11,12,1 3,1 0,1 1.1 2,1 3,1 No path-reset maze and try again OK SOLVE RESET g Walk Wuugil d e , yUU WIII PIUUJUIy Walk UUWII d paull as lal as you Il you dle all n can go. Eventually, you will reach your destination or you won't be able to go any farther. If you can't go any farther, you will need to consider alternative paths. Therefore we need to be able to systematically perform trial and error search. Backtracking is a way for doing just this. It is a systematic, nonrepetitive approach to trying alternative paths and eliminating them if they don't work. Recursion allows you to implement backtracking in a relatively straightforward manner. Each activation frame is used to remember the choice that was made at that particular decision point. After returning from a recursive call you can perform other recursive calls to try out different paths. In this lab assignment, you will be implementing a recursive program to find a path through a maze that employs this backtrack search. The maze itself consists of a two-dimensional grid of colored cells. The maze will start at the cell (0,0) and the exit point is the cell (getNCols()-1.getNRows()-1). The operations get NCols and getNRows() are methods of the class TwoDimGrid. All cells that can be part of Il be in the NON_BACKGROUND color. All the cells that represent barriers and cannot be part of the path will be in the BACKGROUND color. To keep track of a cell that we have visited, we will set it to the TEMPORARY color. If we find a path, all cells on the path will be reset to the PATH color. So there is a total of four possible colors. Initially, the maze has all cells set to color BACKGROUND, as depicted in Fig. 1. The values we will use for each of these constants are as follows and they are defined in Maze class. Green for PATH White for BACKGROUND Red for NON_BACKGROUND Black for TEMPORARY You will use the method getColor() of Maze class to get the color of a cell and its recolor() method to set the color of a cell. You will be completing the recursive method findMazePath() in Maze.java. Maze Test class implements the visual grid and path through the maze using AWT. To test your program you will be running the Maze Test class. When you run the main() method of this class, it will prompt you for the size of the grid, i.e. number of rows and columns, it will display an initial grid as in Fig. 1(a) where all the cells have BACKGROUND color You can edit the grid by pressing on a cell to change its color to NON BACKGROUND (red) as in 1(b). In 1(b), we can move from cell (0,0) to (0,1) but to (1,0); from cell (1,2) we can move left, right and down but not up. When you press solve button, it will call findMazePath you had implememented which will find a path from (0,0) to (4,4) by coloring the cells in the path with color PATH(i.e. green). Fig. 1(a) Fig. 1(b) Fib. 1(c) Toggle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 4,0 0,1 1,1 2,1 3,1 4,1 0, 21, 22, 23,2 0,2 down but not up. When you press solve button, it will call findMazePath you had implememented which will find a path from (0,0) to (4,4) by coloring the cells in the path with color PATH (i.e. green). Fig. 1(a) Fig. 1(b) Fib. 1(c) Toggle a button to change its color Press SOLVE when ready Toogle a button to change its color Press SOLVE when ready Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 3.0 3,0 0,1 0, 11, 12,1 1, 2 0,3 0,4 1,4 2,4 SOLVE RESET SOLVE RESET SOLVE RESET Here is an example of a maze which does not have a path from (0,0) to (4,4). Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2.0 SOLVE RESET When you nin the program you should set the following where cells colored TEMPORARY Toggle a button to change its color Press SOLVE when ready 0,1 1,1 0,2 1,2 0,3 SOLVE RESET When you run the program, you should get the following where cells colored TEMPORARY (Black) have been visited. Toggle a button to change its color Press SOLVE when ready 0,0 1,0 2,0 3,0 Message 0, 11,12,1 3,1 0,1 1.1 2,1 3,1 No path-reset maze and try again OK SOLVE RESET
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started