Question

For this homework you will be making a maze solver. Your program will take in from...

For this homework you will be making a maze solver. Your program will take in from a file 2 things. The size of the square maze, and the maze itself. The maze will consists of numbers between 0 and 3, where 0 is the start of the maze, 1 is an open path, 3 is a wall, and 2 is the end of the maze. For example a 6x6 maze could be represented by the following file, there will be no spaces seperating the elements of the maze:

6
011113
333311
111113
331333
331111
333332

Your program must then solve the maze. It will then output the correct path through the maze marked by 0’s to the command line. For example for the maze above it may output the following solution(I added spaces to make it more readable not necessary):

000003 333301 110003 330333 330000 333330

You can assume that the input contains the exact amount of numbers needed and that it is a solvable maze, following the rules outlined above.

Save your program as a “Homework2.java” file. Your program should read from a file named “in.txt” if you have your program read from a different file you may recieve no credit for this assignment.

1

Your program does not need to find the shortest path, just a path from the start to the finish. For example on the following maze:

3 011 131 211

The following is an acceptable solution to the problem:

000 130 000

While the shortest path is actually the following:

011 031 011

You may also assume that the maze will be no larger than 40x40

1.1 Hints

• Recursion is your friend.
• Store the maze as a 2D array.
• Remember that arrays are pass by reference not value.

2 Extra Credit

For extra credit have your program find the shortest solution.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Arrays;
import java.util.Scanner;

public class Homework2 {

private static int N = 4;

public Homework2(int n) {
N = n;
}

/* A utility function to print solution matrix
sol[N][N] */
void printSolution(int sol[][])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if(sol[i][j] == 1)
sol[i][j] = 0;
System.out.print(sol[i][j]);
}
System.out.println();
}
}

/* A utility function to check if x,y is valid
index for N*N maze */
boolean isSafe(int maze[][], int x, int y)
{
// if (x,y outside maze) return false
return ((x == 0 && y == 0) || (x >= 0 && x < N && y >= 0 &&
y < N && (maze[x][y] == 1 || maze[x][y] == 2)));
}

/* This function solves the Maze problem using
Backtracking. It mainly uses solveMazeUtil()
to solve the problem. It returns false if no
path is possible, otherwise return true and
prints the path in the form of 1s. Please note
that there may be more than one solutions, this
function prints one of the feasible solutions.*/
boolean solveMaze(int maze[][])
{
int sol[][] = new int[N][N];
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
sol[i][j] = maze[i][j];
  
int visted[][] = new int[N][N];
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
visted[i][j] = 0;
if (solveMazeUtil(maze, 0, 0, sol, visted) == false)
{
System.out.println("Solution doesn't exist");
return false;
}

printSolution(sol);
return true;
}

/* A recursive utility function to solve Maze
problem */
boolean solveMazeUtil(int maze[][], int x, int y,
int sol[][], int visited[][])
{
// if (x,y is goal) return true
if (x == N - 1 && y == N - 1)
{
sol[x][y] = 2;
return true;
}

// Check if maze[x][y] is valid
if (isSafe(maze, x, y) == true)
{
// mark x,y as part of solution path
sol[x][y] = 0;
  
/* If moving in x direction doesn't give
solution then Move down in y direction */
if ((y -1) >= 0 && visited[x][y-1] == 0)
{
visited[x][y-1] = 1;
if(solveMazeUtil(maze, x, y - 1, sol, visited))
return true;
}


/* Move forward in x direction */
if ((x+1) < N && visited[x+1][y] == 0)
{
visited[x+1][y] = 1;
if(solveMazeUtil(maze, x+1, y, sol, visited))
return true;
}
if ((y +1) < N && visited[x][y+1] == 0)
{
visited[x][y+1] = 1;
if(solveMazeUtil(maze, x, y + 1, sol, visited))
return true;
}

  
/* If none of the above movements work then
BACKTRACK: unmark x,y as part of solution
path */
sol[x][y] = maze[x][y];
return false;
}

return false;
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of maze: ");
int n = sc.nextInt();
Homework2 hw = new Homework2(n);
int maze[][] = new int [n][n];
sc.nextLine();
for(int i = 0; i < n; i++)
{
String str = sc.nextLine();
for (int j = 0; j < str.length(); j++)
{
maze[i][j] = Integer.parseInt(String.valueOf(str.charAt(j)));
}
}
  
System.out.println();
sc.close();
hw.solveMaze(maze);
}
}

Add a comment
Know the answer?
Add Answer to:
For this homework you will be making a maze solver. Your program will take in from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code a program and reads a maze file and exits non-zero if the maze is not...

    Code a program and reads a maze file and exits non-zero if the maze is not valid. Or outputs the maze in a frame if it is good. The name of the file containg the maze is the only command-line parameter to this program. Read that file with the stdio functions, and close it propperly. A maze is a rectangular array of characters. It is encoded in the file in 2 parts: header line The header has 2 decimal intergers...

  • PLEASE ANSWER IN C++! Given the starting point in a maze, you are to find and...

    PLEASE ANSWER IN C++! Given the starting point in a maze, you are to find and mark a path out of the maze, which is represented by a 20x20 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically or horizontally in any direction that contains a 0; you may not move into a square with a 1. If you move into the square with...

  • PLEASE FOLLOW ALL DIRECTIONS AND CONFIRM BEFORE SUBMITTING ANSWER. EXAMPLES OF HOW THE PROGRAM SHOULD LOOK...

    PLEASE FOLLOW ALL DIRECTIONS AND CONFIRM BEFORE SUBMITTING ANSWER. EXAMPLES OF HOW THE PROGRAM SHOULD LOOK AND WORK ARE DOWN BELOW. I REALLY NEED THIS ANSWERED AS SOON AS POSSIBLE. PLEASE AND THANK YOU!!! 1. Write a Java program that solves a maze Maze is an exciting puzzle game whose goal is to find the path from a starting position (S) to an end position (E ). 2. Important functions for the program Reading a maze file After reading a...

  • URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this...

    URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this maze problem: (No classes please!!!!) A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • In this exercise you will be designing a program in c++ that does the following: Prompts...

    In this exercise you will be designing a program in c++ that does the following: Prompts the user for a file name and then shows the user with the number of word in the file. The program should then allow the user to repeatedly specify a number and the program should show that word in the file. For example, if the file contained the text: Hello. This is the text that is contained in this file. A sample run of...

  • For this lab, you can assume that no string entered by the user will be longer...

    For this lab, you can assume that no string entered by the user will be longer than 128 characters. You can also assume that the database file does not contain more than 128 lines. However, you can not assume that the database has the correct format. There are three commands your program must handle. If any other command is entered, the program should reply Unrecognized command. and then present the prompt again. The three commands are: quit - The program...

  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining...

    Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining laboratory assignments will build on this one, allowing you to improve your initial submission based on feedback from your instructor. The program itself will be capable of reading and writing multiple image formats including jpg, png, tiff, and a custom format: msoe files. The program will also be able to apply simple transformations to the image like: Converting the image to grayscale . Producing...

  • Write and submit one complete Java program to solve the following requirements. Your program will employ...

    Write and submit one complete Java program to solve the following requirements. Your program will employ packages (that is, source directories), and contain multiple source files. Because you are using packages, your code should be in a directory named “Greenhouse.” You should be able to compile your code using the command “javac Greenhouse\*.java” from a directory just below the Greenhouse directory. In the program for this assignment, class names have been specified. You mustuse the supplied class name for both...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT