Java maze The following grid of hashes(#) and dots(.) is a 2D array representati
ID: 3591599 • Letter: J
Question
Java maze
The following grid of hashes(#) and dots(.) is a 2D array representation of a maze
The hashes represent the walls of the maze and the dots represent the possible paths through the maze. Moves can only be made to a location in the array that contains a dot.
(Algorithm Explanation)
There is a simple algorithm for walking through a maze that GUARANTEES finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually you will arrive at the exit of the maze. If you follow the algorithm.
What to do:
Write a program to walk through the maze. You must write a recursive function. The function should recieve as arguments 12-by-12 character array representing the maze and the starting point (x, y location in the maze).
As the function attempts to escape from the maze it should place an X for the current path taken. The program should display the maze at each step so you can watch as the maze is solved. Put an X where you have traveled and maybe an O where you currently are located. The end of the maze will be a capital 'F' (stands for finished).
This algorithm doesn't work for all mazes. Can you come up with a maze where this doesn't work?
(Movement Rules)
*You can only see one step to the left, right and one step in front of you for each turn.
*Each call to the recursive move method can only do one of three things......
1.Take a 90 degree turn to right and step one step, turn and step in one turn so you don't do a infinite loop.
2.Take one step forward.
3.Turn 90 degrees to the left.
(Hints)
Make a move, print out the maze and see where you are, make another move, print the maze. Don't try to solve the whole maze at once, get the different moves working first.
Think of it like this......"I am at x, y, my hand is at Hx, Hy which tells me the direction I'm facing based on Hx being less than or greater than x, and Hy being less than or greater than y. Based on on that logic I can check what my hand is touching and what is in front of me.
1.IF my hand is on a dot I will turn 90 degrees right and move to that dot. I will recursively call my method with a new x, y and a new hx, hy.
2.If my hand is not on a dot, I will look in front of me, if it is a . in front of me I'll move forward and then recursively call this method with my new x, y, and hx, hy, ........
3.If it's a # in front of me I can't go forward so I'll turn 90 degrees to left and call the same method recursively with the same x, y, and new hx, hy.
4..........so on and so on.
Lots of if statements to figure out which way you are facing, where you need to check to see if you can move forward, of if you need to turn.
The maze should be put into a 2D array. I have more mazes I'll link in below.
Also the starting point is always on the outside of the maze, you can search the outside of the input to find a . then you know your starting point. Do that last, wait until you have everything else working first......hard code in the starting point until you get the traversal working.
If you hit a dead end you must turn left, then turn left (2 recursive calls according to the rules) and then head back along the path you already travelled......but now instead of .'s you will see x's if you are showing the route you travelled with x's. Your if statements should handle that......example: if(maze[y+1][x] == '.' || maze[y+1][x] == 'x')
Explanation / Answer
//Defines class Maze
public class Maze
{
//Set the size of the matrix
final int N = 4;
//Method to print solution matrix solution[N][N]
void printSolution(String solution[][])
{
//Loops till N number of rows
for (int r = 0; r < N; r++)
{
//Loops till N number of columns
for (int c = 0; c < N; c++)
System.out.print(" " + solution[r][c] + " ");
System.out.println();
}//End of inner for loop
}//End of method
// Method to check if r,c is valid index for N * N maze
boolean isSafe(String maze[][], int r, int c)
{
// Checks if (r, c outside maze) return false
return (r >= 0 && r < N && c >= 0 && c < N && maze[r][c] == ".");
}//End of method
// Method returns false if no path is possible.
// Otherwise return true
// Prints the solution path in the form of '.' dots.
boolean solveMaze(String maze[][])
{
//Creates a 2D matrix for solution and initializes it to "#"
String solution[][] =
{{"#", "#", "#", "#"},
{"#", "#", "#", "#"},
{"#", "#", "#", "#"},
{"#", "#", "#", "#"}
};
//Checks whether solution available or not
if (solveMazeUtil(maze, 0, 0, solution) == false)
{
System.out.print("Solution doesn't exist");
return false;
}//End of if
// Displays the solution
printSolution(solution);
//Returns true for success
return true;
}//End of method
// Recursive method to give solution to maze
boolean solveMazeUtil(String question[][], int r, int c, String solution[][])
{
// Checks if (r, c is goal) return true
if (r == N - 1 && c == N - 1)
{
// Assigns "." dot to the r and c index position of the solution matrix
solution[r][c] = ".";
//Returns true
return true;
}//End of if
// Check if maze[r][c] is valid
if (isSafe(question, r, c) == true)
{
// Assigns "." dot to the row and column index position of the solution matrix
solution[r][c] = ".";
// Move forward in row direction
if (solveMazeUtil(question, r + 1, c, solution))
return true;
// Checks if moving in r i.e., row direction doesn't give solution then move down in c i.e., column direction
if (solveMazeUtil(question, r, c + 1, solution))
return true;
// Checks if none of the above movements work then use back tracking
// Mark row and column as part of solution path to "#"
solution[r][c] = "#";
//Return false for not success
return false;
}//End of if
return false;
}//End of method
//main method definition
public static void main(String ss[])
{
//Creates an object of the class Maze
Maze rat = new Maze();
//Creates a 2D matrix question for maze and initializes the problem
String question[][] =
{{".", "#", "#", "#"},
{".", ".", "#", "."},
{"#", ".", "#", "#"},
{".", ".", ".", "."}
};
//Calls the method solveMaze() to give solution
rat.solveMaze(question);
}//End of main method
}//End of class
Sample Run:
. # # #
. . # #
# . # #
# . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.