x.Hmprogram which will: * Input the name of a textfile which has a \"maze\" (0=e
ID: 3618243 • Letter: X
Question
x.Hmprogram which will:* Input the name of a textfile which has a "maze" (0=empty square, 1=wall,
with possible spaces and blank lines which you should ignore).
* Input the starting square's row and column indices.
* Input the target square's row and column indices.
* Output if it is/isn't possible to get to the target from the start square.
* Output the solution path (not necessarily the shortest) if it is possible.
----------------------------------------------------------------------
SAMPLE RUN
----------
Consider a text file named "samplemaze10" which has this text:
11111111
10101011
10100001
1 0 1 1 1 0 0 1
1 0 0 1 0 0 0 1
11000111
10000001
11111111
Filename with maze pattern = ? <--- user inputs: samplemaze10
Start, row index = ? <--- user inputs: 6
Start, col index = ? <--- user inputs: 3
Target, row index = ? <--- user inputs: 1
Target, col index = ? <--- user inputs: 1
Target square was hit!
square#1 = 6 3
square#2 = 5 3
square#3 = 5 2
square#4 = 4 2
square#5 = 4 1
square#6 = 3 1
square#7 = 2 1
square#8 = 1 1
----------------------------------------------------------------------
Code:
//filename: Prog6.java
import sdsu.io.*;
import java.util.*;
import java.io.*;
class GridSquare
{
public int i; //row index
public int j; //column index
public GridSquare(int r, int c)
{
this.i = r;
this.j = c;
}
public String toString()
{
return this.i+" "+this.j;
}
}
public class Prog6
{
public static void main(String args[]) throws IOException
{
String filename = sdsu.io.Console.readLine("Filename with maze pattern = ?");
Scanner scanf = new Scanner(new File(filename));
Vector<String> lines = new Vector<String>();
while ( scanf.hasNext() )
{
String s = scanf.nextLine();
s = s.trim();
if (! s.equals(""))
{ s=s.replace(" ",""); lines.addElement(s); }
}
scanf.close();
int numberOfRows = lines.size();
int numberOfCols = (lines.elementAt(0)).length();
int[][] arr = new int[_____][_____];
for (int r=0; r<_____; r++)
{
String t = lines.elementAt(r);
for (int c=0; c<_____; c++)
{
arr[r][c] = Integer.parseInt(t.charAt(c)+"");
}
}
//Uncomment these lines if you'd like program to output the maze:
//for (int r=0; r<numberOfRows; r++)
//{
// System.out.println(Arrays.toString(arr[r]));
//}
int starti = sdsu.io.Console.readInt("Start, row index = ?"); //eg, 6
int startj = sdsu.io.Console.readInt("Start, col index = ?"); //eg, 3
int targeti = sdsu.io.Console.readInt("Target, row index = ?"); //eg, 1
int targetj = sdsu.io.Console.readInt("Target, col index = ?"); //eg, 1
Stack<GridSquare> st1 = new Stack<GridSquare>();
Stack<GridSquare> solnpath = new Stack<GridSquare>();
//using array encoding:
// 0 = walkable
// 1 = wall
// 2 = walkable, but was visited
GridSquare startsqr = new _____(_____,_____);
st1._____( _____ );
arr[_____][_____] = 2; //move to start square
solnpath._____( _____ );
boolean reachedtarget = _____;
while ( st1.isEmpty()==_____ && reachedtarget==_____ )
{
GridSquare curr = st1.peek();
if ( freemove(_____,_____,_____)==_____ )
{
GridSquare loc = null;
if ( upisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( downisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( leftisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( rightisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
st1._____(_____);
arr[_____][_____] = 2; //move to loc
solnpath._____(_____);
if ( _____==_____ && _____==_____ )
{
reachedtarget=_____;
continue;
}
}
else
{
GridSquare ss = st1._____;
arr[_____][_____] = 2; //move to ss
solnpath._____;
}
} //end while
System.out.println();
if ( reachedtarget==_____ )
System.out.println("Target square was hit!");
else
System.out. Code:
//filename: Prog6.java
import sdsu.io.*;
import java.util.*;
import java.io.*;
class GridSquare
{
public int i; //row index
public int j; //column index
public GridSquare(int r, int c)
{
this.i = r;
this.j = c;
}
public String toString()
{
return this.i+" "+this.j;
}
}
public class Prog6
{
public static void main(String args[]) throws IOException
{
String filename = sdsu.io.Console.readLine("Filename with maze pattern = ?");
Scanner scanf = new Scanner(new File(filename));
Vector<String> lines = new Vector<String>();
while ( scanf.hasNext() )
{
String s = scanf.nextLine();
s = s.trim();
if (! s.equals(""))
{ s=s.replace(" ",""); lines.addElement(s); }
}
scanf.close();
int numberOfRows = lines.size();
int numberOfCols = (lines.elementAt(0)).length();
int[][] arr = new int[_____][_____];
for (int r=0; r<_____; r++)
{
String t = lines.elementAt(r);
for (int c=0; c<_____; c++)
{
arr[r][c] = Integer.parseInt(t.charAt(c)+"");
}
}
//Uncomment these lines if you'd like program to output the maze:
//for (int r=0; r<numberOfRows; r++)
//{
// System.out.println(Arrays.toString(arr[r]));
//}
int starti = sdsu.io.Console.readInt("Start, row index = ?"); //eg, 6
int startj = sdsu.io.Console.readInt("Start, col index = ?"); //eg, 3
int targeti = sdsu.io.Console.readInt("Target, row index = ?"); //eg, 1
int targetj = sdsu.io.Console.readInt("Target, col index = ?"); //eg, 1
Stack<GridSquare> st1 = new Stack<GridSquare>();
Stack<GridSquare> solnpath = new Stack<GridSquare>();
//using array encoding:
// 0 = walkable
// 1 = wall
// 2 = walkable, but was visited
GridSquare startsqr = new _____(_____,_____);
st1._____( _____ );
arr[_____][_____] = 2; //move to start square
solnpath._____( _____ );
boolean reachedtarget = _____;
while ( st1.isEmpty()==_____ && reachedtarget==_____ )
{
GridSquare curr = st1.peek();
if ( freemove(_____,_____,_____)==_____ )
{
GridSquare loc = null;
if ( upisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( downisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( leftisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
else if ( rightisfree(_____,_____,_____)==_____ )
loc = new _____(_____,_____);
st1._____(_____);
arr[_____][_____] = 2; //move to loc
solnpath._____(_____);
if ( _____==_____ && _____==_____ )
{
reachedtarget=_____;
continue;
}
}
else
{
GridSquare ss = st1._____;
arr[_____][_____] = 2; //move to ss
solnpath._____;
}
} //end while
System.out.println();
if ( reachedtarget==_____ )
System.out.println("Target square was hit!");
else
System.out.
Explanation / Answer
bump , will rate lifersaver!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.