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

double scripted array representation of a terrain of size 12 x 12. 1 1 1 1 1 1 1

ID: 3665753 • Letter: D

Question

double scripted array representation of a terrain of size 12 x 12.

1 1 1 1 1 1 1 1 1 1 1 1

1 0 0 0 1 0 0 0 0 1 0 1

0 0 1 0 1 0 0 0 0 1 0 1

1 1 0 0 1 0 0 0 0 1 0 1

1 0 0 0 0 0 1 1 0 1 0 0

1 1 1 1 0 0 1 1 0 1 0 1

1 0 1 1 0 0 1 1 0 1 0 1

1 1 1 1 0 0 1 1 0 1 0 1

1 0 0 0 0 0 0 0 0 1 0 1

1 1 1 1 1 0 0 0 0 1 0 1

1 0 0 0 0 0 0 0 0 1 0 1

1 1 1 1 1 1 1 1 1 1 1 1

The ones represent the obstacles if the field, and the zeroes represent positions in the possible path through the field. In this assignment, the field will have a single entrance and a single exit, i.e., there will be only two zeroes in the “outer wall” of the field. In order to find the entrance and the exit, traverse the outer wall of the field in the clockwise direction, starting from the upper left corner. The first encountered zero will be the entrance (square [4,11] in the field above), and next zero will be the exit (square [2,0]). In this assignment, the size of a field is NOT FIXED. Each array dimension will vary in size between 5 and 100. There are several simple algorithms for walking through a field that guarantee finding the path, if one exists. The only legal moves are north, west, south or east (no diagonal moves). For example, look to your right and walk forward. Always keep the obstacle to your right. If you reach the corner of the obstacle, turn right and continue following its “border” on your right side. There may be a shorter path than the one you have taken, but in this way you are guaranteed to get out of the field. In this algorithm, if you exit from the field through the entrance, this means that the path from the entrance to the exit does not exist. Otherwise, the algorithm has found a path that avoids all obstacles. In your assignment, you need to write a program called path.c. First of all, the program should ask user to type in the size of the field (if you enter 12, 6, this will imply a 12 rows and 6 columns field). Then the program should request the user to type in the name of the input file that contains the field. You may assume that the field size given to the program always matches the size in the file. The field will be given to your program in an ASCII text file, looking very much like the one above. Upon opening the file, your program must find the entrance. As your program attempts to find a path through the field, it should place the character X into each square visited in the path. Note that your program MUST NOT replace a 1 (an obstacle) with an X. Only zeroes can be replaced by X. Before exiting, your program must display the field with the traversed path, that is, the path between the entrance and the exit (the path is marked by X). Your program must also report whether the path was found or not.

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author prmsh
*/
public class Chegg {

/**
* @param args the command line arguments
*/
public static boolean solve(String[][] maze, int row, int column) {

boolean done = false;

if (valid(maze,row, column)) {

maze[row][column] = "X"; // cell has been tried

if (row == maze.length - 1 && column == maze[0].length - 1) {
//mazr solves
done = true;
printMaze(maze,row,column);
} else {
done = solve(maze,row + 1, column); // check down
if (!done) {
done = solve(maze,row, column + 1); // check right
}
if (!done) {
done = solve(maze,row - 1, column); // check up
}
if (!done) {
done = solve(maze,row, column - 1); //check left
}
}
if (done) // part of the final path
{
maze[row][column] = "Y";
}
}

return done;

} // method solve

//===========================================================
// Determines if a specific location is valid.
//===========================================================
private static boolean valid(String[][] maze, int row, int column) {

boolean result = false;

// check if cell is in the bounds of the matrix
if (row >= 0 && row < maze.length
&& column >= 0 && column < maze[0].length) // check if cell is not blocked and not previously tried
{
if (maze[row][column] == "1") {
result = true;
}
}

return result;

} // method valid

//printing maze
public static void printMaze(String[][] maze, int row, int column){
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Row dimensions: ");
int row = sc.nextInt();
System.out.println("Enter Column dimensions: ");
int col = sc.nextInt();
//declaring array
String[][] maze = new String[row][col];

//now reading file
File file = new File("C:\Users\prmsh\Documents\NetBeansProjects\Chegg\src\chegg\Maze.txt");
try {
Scanner scan = new Scanner(file);
int temp = 0;
while (scan.hasNextLine()) {
String line = sc.nextLine();
//splitting line by space
String[] data = line.split(" ");
System.out.println(data.length);
for (int i = 0; i < col; i++) {
maze[temp][i] = data[i];
}
temp++;
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Maze solved: "+solve(maze,row,col));
}
}