WRITE IN JAVA PLEASE NOT C++. Modify the maze in the MazeSearch.java ( Author: L
ID: 3685968 • Letter: W
Question
WRITE IN JAVA PLEASE NOT C++.
Modify the maze in the MazeSearch.java ( Author: Lewis/Loftus) program, such that
1. The maze cannot be traversed.
2. There are 2 different paths to traverse the maze.
Do not modify any other part of the program except for the maze.
This is the original maze:
{1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1} };
After each modification, run the program and see if it works and what it does. Submit the program with the modified mazes. Write a few lines of text that explain what the program did when you ran it after modofication 1 and after modification 2.
Explanation / Answer
1. Maze Cannot be traversed:
public class Maze_Search {
//===========================================================
// Creates a new maze, prints its original form, attempts
// to solve it, and prints out its final form.
//===========================================================
public static void main (String[] args) {
Maze labyrinth = new Maze();
labyrinth.print_maze();
if (labyrinth.solve(0, 0))
System.out.println ("Maze solved!");
else
System.out.println ("No solution.");
labyrinth.print_maze();
} // method main
} // class Maze_Search
class Maze {
private final int TRIED = 3;
private final int PATH = 7;
int[][] grid = {{1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
//===========================================================
// Prints the maze grid.
//===========================================================
public void print_maze () {
System.out.println();
for (int row=0; row < grid.length; row++) {
for (int column=0; column < grid[row].length; column++)
System.out.print (grid[row][column]);
System.out.println();
}
System.out.println();
} // method print_maze
public boolean solve (int row, int column) {
boolean done = false;
if (valid (row, column)) {
grid[row][column] =TRIED ; // cell has been tried
if (row == grid.length && column == grid[0].length)
done = true; // maze is solved
else {
done = solve (row+1, column); // down
if (!done)
done = solve (row, column+1); // right
if (!done)
done = solve (row-1, column); // up
if (!done)
done = solve (row, column-1); // left
}
if (done) // part of the final path
grid[row][column] = 7;
}
return done;
} // method solve
//===========================================================
// Determines if a specific location is valid.
//===========================================================
private boolean valid (int row, int column) {
boolean result = false;
// check if cell is in the bounds of the matrix
if (row >= 0 && row < grid.length &&
column >= 0 && column < grid[0].length)
// check if cell is not blocked and not previously tried
if (grid[row][column] == 1)
result = true;
return result;
} // method valid
} // class Maze
2. The second way:
package com.raj.dreampack;
public class Maze_Search {
//===========================================================
// Creates a new maze, prints its original form, attempts
// to solve it, and prints out its final form.
//===========================================================
public static void main (String[] args) {
Maze labyrinth = new Maze();
labyrinth.print_maze();
if (labyrinth.solve(0, 0))
System.out.println ("Maze solved!");
else
System.out.println ("No solution.");
labyrinth.print_maze();
} // method main
} // class Maze_Search
class Maze {
private final int TRIED = 7;
private final int PATH = 3;
int[][] grid = {{1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
//===========================================================
// Prints the maze grid.
//===========================================================
public void print_maze () {
System.out.println();
for (int row=0; row < grid.length; row++) {
for (int column=0; column < grid[row].length; column++)
System.out.print (grid[row][column]);
System.out.println();
}
System.out.println();
} // method print_maze
public boolean solve (int row, int column) {
boolean done = false;
if (valid (row, column)) {
grid[row][column] =TRIED ; // cell has been tried
if (row == grid.length-1 && column == grid[0].length-1)
done = true; // maze is solved
else {
done = solve (row+1, column); // down
if (!done)
done = solve (row, column+1); // right
if (!done)
done = solve (row-1, column); // up
if (!done)
done = solve (row, column-1); // left
}
if (done) // part of the final path
grid[row][column] = 7;
}
return done;
} // method solve
//===========================================================
// Determines if a specific location is valid.
//===========================================================
private boolean valid (int row, int column) {
boolean result = false;
// check if cell is in the bounds of the matrix
if (row >= 0 && row < grid.length &&
column >= 0 && column < grid[0].length)
// check if cell is not blocked and not previously tried
if (grid[row][column] == 1)
result = true;
return result;
} // method valid
} // class Maze
Hope this will help. I have modified the conditions and the trys.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.