Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing this java code. If anyone could help me solve it that would be great! Thanks! The Assignment Project #9 requires you

I need help writing this java code. If anyone could help me solve it that would be great! Thanks!

The Assignment

Project #9 requires you to find an escape path from a given starting point in a maze. The maze (or swamp or mine field etc.) is a square 2D array of int. A value of zero in any cell represents a wall (land mine or quicksand etc.) while a value of one represents a possible step to take. Each input file will have a line at the top of the file that has the square dimension of the matrix followed by the drop in point. The drop in point is the row/col pair indicating where you are dropped into the swamp. You escape the swamp by taking one step at a time to any adjacent square that is a 1. You are not allowed to step on a zero cell. You must somehow remember which squares you have already stepped on in a path so that you don't get into an infinite loop. The first input file, swamp0.txt is a simple swamp that has only one path and that path lead directly to the edge of the swamp (escape) with no alternative paths, dead ends or cycles to get caught up ion. If you solve that one you get a 70% for project #8. It is easy to write a solution for swamp0 that does not use recursion. The other 3 input files (swamp1,2 and 3) contain paths that are of incresing complexity containing dead ends and/or cycles. These swamps cannot be easily solved without using recursion.

The starter file that you are given has two methods already written for you. The first given method loads the grid values and generates the 2D int array to hold the 1s and 0s. The second method echo's out the swamp to the screen before calling the method that you must fill in - which will print all the escape paths.

What You'll Need

Here are your input files. You cannot get credit for a higher (more difficult) input file unless your program correctly solves the lower (easier) files.

70% credit: input0.txt 1 simple path out. No dead ends. No cycles

80% credit: input1.txt 1 path out + dead ends. No cycles

90% credit: input2.txt multiple paths + dead ends. No cycles

100% credit: input3.txt multiple paths + dead ends + cycles

Here is what the output should look like for the above inputs:

expectedOutputs.txt

The Assignment

You are given this starter file: Project9.java

< >Try not to modify main any more than needed.

Do not modify the methods that load or print the grid.

You are just to write and call the method(s) that print all escape paths.

the only output you are to write is one line per escape path and it must be in the exact format as my sample output. If there are no escape paths then your code prints out no additional output.

Your escape path lines may be in a different order than mine but the row,col values must match exactly in matching lines.

Your program must find and print ALL the escape paths. Lines may be in any order though.

Do not print any other output of any kind other than what my solution demo does.

DO NOT PRINT "DEAD END" OR *ANY* OUTPUT IF THERE ARE NO PATHS OUT.DFS.java

STARTER FILE:

import java.io.*; import java.util.*; // DO NOT IMPORT JAVA.LANG; public class Project9 // the swamp { public static void main(String[] args) throws Exception { int[] dropInPt = new int[2]; // row and col will be on the 2nd line of input file; int[][] swamp = loadSwamp( args[0], dropInPt ); int row=dropInPt[0], col = dropInPt[1]; // DECLARE ANY NEEDED VARIABLE THAT WILL BE PASSED INTO YOUR METHOD THAT FINDS/PRINTS PATHS OUT // CALL YOUR METHOD THAT FINDS/PRINTS PATHS OUT } // END MAIN // ################################################### // DO NOT MODIFY THIS METHOD // ---------------------------------------------------------------- private static int[][] loadSwamp( String infileName, int[] dropInPt ) throws Exception { Scanner infile = new Scanner( new File(infileName) ); int rows=infile.nextInt(); int cols = rows; // ASSUME A SQUARE GRID dropInPt[0]=infile.nextInt(); dropInPt[1]=infile.nextInt(); int[][] swamp = new int[rows][cols]; for(int r = 0; r < rows ; r++) for(int c = 0; c < cols; c++) swamp[r][c] = infile.nextInt(); infile.close(); return swamp; } // END LOAD SWAMP // DO NOT MODIFY THIS METHOD - IT IS JUST FOr DEBUGGING. // ---------------------------------------------------------------- private static void printSwamp(String label, int[][] swamp ) { System.out.println( label ); System.out.print(" "); for(int c = 0; c < swamp.length; c++) System.out.print( c + " " ) ; System.out.print( " "); for(int c = 0; c < swamp.length; c++) System.out.print("- "); System.out.print( " "); for(int r = 0; r < swamp.length; r++) { System.out.print( r + "| "); for(int c = 0; c < swamp[r].length; c++) System.out.print( swamp[r][c] + " "); System.out.println("|"); } System.out.print( " "); for(int c = 0; c < swamp.length; c++) System.out.print("- "); System.out.print( " "); } // END PRINT SWAMP METHOD } // E N D S W A M P C L A S S 

Expected output:

 C:\Users\tlh\Desktop>java Project9 input0.txt [2,3][3,3][4,4][5,5][6,6][7,7][8,8][7,9] C:\Users\tlh\Desktop>java Project9 input1.txt [2,3][3,3][4,2][5,1][6,0] C:\Users\tlh\Desktop>java Project9 input2.txt [4,4][3,5][2,4][1,4][0,4] [4,4][4,3][5,2][6,2][7,1][8,0] C:\Users\tlh\Desktop>java Project9 input3.txt [1,1][1,2][2,3][1,4][0,4] [1,1][1,2][2,3][3,4][4,5][5,4][6,3][7,3] [1,1][1,2][2,3][3,2][4,1][5,2][6,3][7,3] path oputput lines may be in any order but the each individual path must have the [r,c] blocks in exact correct order for a valid path 

input0.txt:

10 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 

input1.txt:

10 2 3

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 

input2.txt:

9 4 4

0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 

input3.txt:

8 1 1

0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How does RFID compare to bar-coding?

Answered: 1 week ago

Question

=+What do you wish you had known when you were starting out?

Answered: 1 week ago