98 943
Assignments Done
98.8%
Successfully Done
In May 2021

Answer to Question #18883 in Java | JSP | JSF for Tolu Banjo

Question #18883
i am writing a 2D array of integers to represent a swamp, heres the question
write a 2D array of integers to represent a swamp. Every value of 0 represents quicksand while every value of 1 represents solid ground. Your job will be to follow the trail of 1's (solid ground) to the edge of the swamp - or until it dead ends. You will take one step at a time in any direction as long as that step is onto solid ground ( 1 ), and not into quicksand ( 0 ). Put simply, you are dropped into the swamp onto a block of solid ground and starting there must follow the trail of safe steps one at a time until you either reach the edge of the swamp (rescued) or reach a dead end (stranded). You are guaranteed the following: 1) There are no cycles in your path to freedom or dead end (thus no backtracking/recursion needed). 2) You will start out on a block of solid ground. 3) There is only one possible path to take, and thus only one possible correct output for any given swamp.

heres what i have so far....

/*
Program9.java

*/

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

public class Program9
{
public static void main(String[] args)
{
int myRow=2, myCol=3; // you are dropped into the swamp at: [2][3]

int[][] swamp1 =
{
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,1 },
{ 0,0,0,1,0,0,0,0,1,0 },
{ 0,0,0,1,0,0,0,1,0,0 },
{ 0,0,0,0,1,0,1,0,0,0 },
{ 0,0,0,0,1,0,0,1,0,0 },
{ 0,0,0,0,1,0,0,0,1,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,0,0 }
};

printSwamp( &quot;SWAMP1&quot;,swamp1 );
printEscapePath( swamp1, myRow,myCol );

// Now swamp2

myRow=2; myCol=8; // you are dropped into the swamp at [2][8]

int[][] swamp2 =
{
{ 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,1,1,0 },
{ 0,0,0,1,0,0,1,0,0,0 },
{ 0,0,0,0,1,1,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 }
};

printSwamp( &quot;\nSWAMP2&quot;,swamp2 );
printEscapePath( swamp2, myRow,myCol );

} // END MAIN

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

private static void printSwamp(String label, int[][] swamp )
{

System.out.println( label );
for(int r = 0; r &lt; swamp.length; r++)
{
for(int c = 0; c &lt; swamp[r].length; c++)
System.out.print( swamp[r][c] + &quot; &quot;);
System.out.println();
}
}

private static void printEscapePath(int[][] swamp, int myRow, int myCol)
{

int[] coords = new int[2];
coords[0]=myRow;
coords[1]=myCol;

System.out.println(&quot;STARTING AT: &quot; + &quot;[&quot;+ myRow + &quot;][&quot; + myCol + &quot;]&quot;);

while ( !onEdge( swamp, coords ) )
{

if ( nextStep( swamp, coords ) )
{
System.out.println( &quot;STEPPED TO: [&quot; + coords[0] + &quot;][&quot; + coords[1] + &quot;]&quot; );
}
else // YOU'RE CROC MEAT :=(
{
System.out.println( &quot;STRANDED AT: [&quot; + coords[0] + &quot;][&quot; + coords[1] + &quot;]&quot; );
return;

} // END WHILE NOT OUT OF SWAMP YET

System.out.println( &quot;ESCAPED AT: [&quot; + coords[0] + &quot;][&quot; + coords[1] + &quot;]&quot; );

} // END PRINT ESCAPE PATH

// ########################################&hellip;
This is what i have so far, now how do i write the method that

// LOOKS AT EVERY ADJACENT SQUARE TO SEE IF IT'S A 1
// IF IT IS, THEN THE CURRENT X,Y IS CHANGED TO -1 (i.e. BEEN HERE)
// AND THE COORDS ARE CHANGED TO THOSE OF THE NEARBY SQAURE WITH A 1 IN IT
// IF NO SAFE STEP IS FOUND THEN JUST RETURN FALSE.
1
2012-11-19T08:46:29-0500

Submit it with all requirements as an assignment to our control panel and we'll assist you.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!