Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

input0.txt: input1.txt: input2.txt: input3.txt: Swamp.java: The Assignment Proje

ID: 3699507 • Letter: I

Question

input0.txt: input1.txt: input2.txt: input3.txt: Swamp.java: 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 ctc) 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 matris followed by the drop in point. The drop in point is the rowicol 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 somchow remember which squares you have already stepped on in a path so that you don't get into an infinite loop. The five input files you are given have cycles and dead ends and valid paths to the edge of the swamp The starter file that you are given has one method already written for you that print the grid out in rectangular format. The second method load the grid from the input file. You must write this method using the printSWamp for clues printSwamp is NOT to be called in the code you hand in. It is just for debugging. Remove any calls to it from your handin version. 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: innumti l simple path out. No dead ends. No cycles * 80% credit: in utLM l path out. Dead ends. No cycles . 9 % credit: mput2w multiple paths out. Dead ends. No cycles * 100% credit: input multiple paths out. Dead Ends. Cycles Here is what the output should look like for the above inputs if I run it separately on all four files: UsersItimDesktop)java Swamp inpute.txt [2,3J3,3J[4,4j[s,sj[6,6107,7][8,8]07,9] :UserstimDesktop>java Swamp input1.txt [2,3]3,3][4,2][5,1j6,e] c:Users imDesktop>java Swamp input2.txt [4,4][3,5][2,4][1,41te,4] [4,4104,35,2][6,2[7,18,0] Users imDesktop>java Swamp input3.txt [1,,2][2,3][1,410e,4] 1,,2[2,3]3,4][4,5][5,416,31[7,3] ,22,3]3,21[4,1]5,2][6,3117,3] :Users imDesktop» You are given this starter file: Swamp.java Do not modify main any more than needed Do not modify the given methods that load or print the grid. You are just to write and call the method(s) that print all escape paths.

Explanation / Answer

import java.io.*;
import java.util.*;

public class 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];

        printSwamp(          "    SWAMP: dropped in at: ["+row+","+col+"] ",swamp );
        System.out.println("    ESCAPE PATHS: ");
      
        String path = "";
        dfs(row,col,swamp,path);

    } // END MAIN
  
    private static void dfs(int r, int c, int[][] swamp, String path)
    {
        path += "["+Integer.toString(r)+ "," + Integer.toString(c)+ "]";

        if (r == swamp.length-1 || c == swamp[0].length-1 || r == 0 || c == 0)
        {
            System.out.println(path);
            return;
        }

        if(swamp[r-1][c] == 1)
        {
            swamp[r][c] = -1;
            dfs(r-1, c, swamp, path);
            swamp[r][c] = 1;
        }
        if(swamp[r-1][c+1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r-1,c+1, swamp, path);
            swamp[r][c] = 1;
        }
        if(swamp[r][c+1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r,c+1,swamp,path);
            swamp[r][c] = 1;
        }
        if(swamp[r+1][c+1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r+1, c+1, swamp, path);
            swamp[r][c] = 1;
        }
        if(swamp[r+1][c] == 1)
        {
            swamp[r][c] = -1;
            dfs(r+1,c,swamp,path);
            swamp[r][c] = 1;
        }
        if(swamp[r+1][c-1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r+1,c-1,swamp,path);
            swamp[r][c] = 1;
        }
        if(swamp[r][c-1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r,c-1,swamp,path);
            swamp[r][c] = 1;
        }
        if(swamp[r-1][c-1] == 1)
        {
            swamp[r][c] = -1;
            dfs(r-1,c-1,swamp,path);
            swamp[r][c] = 1;
        }


    }
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( " ");
    }

   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;
    }
}