Project 8 requires you to find an escape path from a given s

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 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 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.

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.

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. JUST PRINT NOTHING!

input file:

output should look like:

Solution

input0.txt

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


Project2.java

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

public class Project2
{
    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:\ \");

       // YOUR CODE HERE. DECLARE WHATEVER OBJECTS AND VARIABLES NEEDED
       // CALL YOUR METHOD(s) TO PRINT ALL ESCAPE PATHS
       printPath(swamp, dropInPt);
   } // END MAIN

   // ###################################################

   // DO NOT MODIFY THIS METHOD
   // ----------------------------------------------------------------
   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( \"\ \");
    }

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

   private static void printPath(int[][] swamp, int[] dropInPt)
   {
       int[] point = new int[2];
       point[0]=dropInPt[0];
       point[1]=dropInPt[1];
       if (point[0]==0 || point[0]==swamp.length-1 || point[1]==0 || point[1]==swamp[1].length-1)
           System.out.println(\"[\"+ point[0] +\",\"+ point[1] +\"]\");
       else
       {
           ArrayList<String> pathes = new ArrayList<String>();
           String path = \"[\"+ point[0] +\",\"+ point[1] +\"]\";
          
           pathes.add(path);
           for (int i=0; i< pathes.size(); i++)
                   go(swamp, pathes, pathes.get(i));
       }
   }

   private static void go(int[][] swamp, ArrayList<String> pathes, String path)
   {
       int point[] = new int[2];
       point[0]=Character.getNumericValue(path.charAt(path.length()-4));
       point[1]=Character.getNumericValue(path.charAt(path.length()-2));
       if (point[0]==0 || point[0]==swamp.length-1 || point[1]==0 || point[1]==swamp[1].length-1)
           System.out.println(path);
       else
       {
           if (nextStep(swamp, point, pathes, path))
           {
               if (!path.contains(\"[\"+ point[0] +\",\"+ point[1] +\"]\"))
                   path= path+\"[\"+ point[0] +\",\"+ point[1] +\"]\";
               if (point[0]==0 || point[0]==swamp.length-1 || point[1]==0 || point[1]==swamp[1].length-1)
                   System.out.println(path);
               else
                   go(swamp, pathes, path);
           }
       }
   }

   private static boolean nextStep(int[][] swamp, int[] point, ArrayList<String> pathes, String path)
   {
       boolean next = false;
       int count = 0;
       int row = point[0];
       int col = point[1];
       for (int i=row-1; i<=row+1; i++)
       {
           for (int j=col-1; j<=col+1; j++)
           {
               if ((i!= row||j!=col) && swamp[i][j]==1)
               {
                   if (!path.contains(\"[\"+ i +\",\"+ j +\"]\"))
                   {
                       next= true;
                       count++;
                       if (count>1)
                       {
                           String pathNew = new String(path);
                           pathNew = pathNew+\"[\"+ i +\",\"+ j +\"]\";
                           pathes.add(pathNew);
                       }
                       else
                       {
                           point[0]=i;
                           point[1]=j;
                       }
                   }      
               }
           }
       }
       count= 0;
       return next;
   }
}

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 valu
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 valu
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 valu
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 valu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site