Question
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
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 separating 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 0s 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):
0 | 0 | 0 | 0 | 0 | 3 |
3 | 3 | 3 | 3 | 0 | 1 |
1 | 1 | 0 | 0 | 0 | 3 |
3 | 3 | 0 | 3 | 3 | 3 |
3 | 3 | 0 | 0 | 0 | 0 |
3 | 3 | 3 | 3 | 3 | 0 |
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.
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:
0 0 0
1 3 0
0 0 0
While the shortest path is actually the following:
0 1 1
0 3 1
0 1 1
You may also assume that the maze will be no larger than 40x40
"I need need the code to look very similar to this please" some of my classmates have already posted this and I can't have the same code.
public class homework2 { public static void printArray(int[] a) { for(int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void recArray(int[] a, int c) { if(c == a.length) { System.out.println(c); } else { //System.out.println("Start of " + c + " call"); a[c] = 1; System.out.print(c + ":"); printArray(a); recArray(a, c+1); a[c] = 0; System.out.print(c + ":"); printArray(a); //System.out.println("end of " + c + " call"); } } public static void main(String[] args) { System.out.println("Start of Main!"); int[] r = {0,0,0,0,0,0,0}; //printArray(r); recArray(r,0); //printArray(r); System.out.println("Main is done!"); } }
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