Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project #8 requires you to find an escape path from a given starting point in a maze. The maze (or swamp or mine field etc.)

Project #8 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.

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.

Starter File

import java.io.*; import java.uti.*; // 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 

input file

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 

output should look like:

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 

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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago