In: Computer Science
PLEASE ANSWER IN C++
Given the starting point in a maze, you are to find and mark a path out of the maze, which is represented by a 20x20 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically or horizontally in any direction that contains a 0; you may not move into a square with a 1. If you move into the square with an E, you have exited the maze. If you are in a square with 1s on three sides, you must go back the way you came and try another path. You may not move diagonally. For this program, use can ONLY use a single linked list.
Program Requirements: Your program should use single linked list ONLY for finding the path. Input of program: Input is the following array of characters (1s, 0s, and E) from an ASCII text data file (maze.txt); as follows:
E0001110000000100100
11100011101110001111
11111000101000111000
00001110101100010010
01011000101111000110
00001110000110011110
11011100110110111000
00011110110111111101
01011011110110100001
01000000000110110111
11011011010010000000
01010010011000101011
01111000101110101110
00001110000111011001
01101011101101000011
11000110100111011010
01110000100100110011
11010111110110000000
01110100011000111110
00011001000011100010
Each data line consists of one row of maze. Starting points (i.e. a row, column pair) in the maze will be input from the keyboard.
Output of program: Echo print the maze complete with numbered rows and columns prior to asking the user for their starting point. For each entry into the maze, print the complete maze with a S in the starting point followed by the words ‘I am free’ if you have found a path out of the maze or the words ‘Help, I am trapped’ if you cannot. Your output could be in two formats: Print the path (by using a series of pluses (+)) you took through the maze should one be found OR Print the path as a single linked list. A program heading in the following format should begin your program:
// Program Name
// <your name>
// <date>
Here is Complete Code, Just Copy and Run :
import java.util.Scanner;
public class Main
{
private static final char WALL = '1';
private static final char PATH = '0';
private static final char EXIT = 'E';
private static final char START = 'S';
private static final char VISITED = 'X';
private static final char ANSWER = '+';
private static Scanner keyboard;
private static String readString(final String prompt) {
String value;
while (true) {
System.out.print(prompt);
value = Main.keyboard.nextLine().trim();
if (!value.isEmpty()) {
break;
}
System.out.println("Error: Please enter a value.");
}
return value;
}
private static int readInt(final String prompt) {
while (true) {
try {
System.out.print(prompt);
return Integer.parseInt(Main.keyboard.nextLine());
}
catch (Exception e) {
System.out.println("Error: Please enter a numeric value.");
continue;
}
}
}
private static char[][] loadMaze() throws Exception {
final char[][] maze = new char[4][4];
try {
for (int row = 0; row < maze.length; ++row) {
for (int column = 0; column < maze.length; ++column) {
maze[row][column] = Main.keyboard.next().charAt(0);
if (maze[row][column] != '0' && maze[row][column] != '1'
&& maze[row][column] != 'E') {
throw new Exception("Invalid map.");
}
}
}
}
catch (Exception e2) {
throw new Exception("Invalid map.");
}
return maze;
}
private static Coordinate readStartingCoordinate(final char[][]
maze) {
int startingRow = 0;
int startingColumn = 0;
while (true) {
for (startingRow = readInt("Enter starting row: "); startingRow
< 0 || startingRow >= 20; startingRow = readInt("Enter
starting row: ")) {
System.out.println("Error: Please enter a value from 0 to
19");
}
for (startingColumn = readInt("Enter starting column: ");
startingColumn < 0 || startingColumn >= 20; startingColumn =
readInt("Enter starting column: ")) {
System.out.println("Error: Please enter a value from 0 to
19");
}
if (maze[startingRow][startingColumn] != '1') {
break;
}
System.out.println("Error: The starting point you selected is a
wall");
}
return new Coordinate(startingRow, startingColumn);
}
private static boolean solveMaze(final char[][] maze, final
LinkedList path) {
if (path.isEmpty()) {
return false;
}
final Coordinate coordinate = path.peek();
final int row = coordinate.getRow();
final int column = coordinate.getColumn();
if (maze[row][column] == 'E') {
return true;
}
maze[row][column] = 'X';
if (row - 1 >= 0 && (maze[row - 1][column] == '0' ||
maze[row - 1][column] == 'E')) {
path.push(new Coordinate(row - 1, column));
if (solveMaze(maze, path)) {
return true;
}
}
if (row + 1 < maze.length && (maze[row + 1][column] ==
'0' || maze[row + 1][column] == 'E')) {
path.push(new Coordinate(row + 1, column));
if (solveMaze(maze, path)) {
return true;
}
}
if (column - 1 >= 0 && (maze[row][column - 1] == '0' ||
maze[row][column - 1] == 'E')) {
path.push(new Coordinate(row, column - 1));
if (solveMaze(maze, path)) {
return true;
}
}
if (column + 1 < maze.length && (maze[row][column + 1]
== '0' || maze[row][column + 1] == 'E')) {
path.push(new Coordinate(row, column + 1));
if (solveMaze(maze, path)) {
return true;
}
}
path.pop();
return false;
}
private static void cleanMaze(final char[][] maze, final LinkedList
path, final Coordinate start) {
for (int row = 0; row < maze.length; ++row) {
for (int column = 0; column < maze.length; ++column) {
if (maze[row][column] == 'X') {
maze[row][column] = '0';
}
}
}
final Coordinate end = path.pop();
maze[end.getRow()][end.getColumn()] = 'E';
while (!path.isEmpty()) {
final Coordinate coordinate = path.pop();
maze[coordinate.getRow()][coordinate.getColumn()] = '+';
}
maze[start.getRow()][start.getColumn()] = 'S';
}
private static void printMaze(final char[][] maze) {
System.out.printf("%3s", " ");
for (int i = 0; i < maze.length; ++i) {
System.out.printf("%3d", i);
}
System.out.println();
for (int row = 0; row < maze.length; ++row) {
System.out.printf("%-3d", row);
for (int column = 0; column < maze.length; ++column) {
System.out.printf("%3c", maze[row][column]);
}
System.out.println();
}
System.out.println();
}
public static void main(final String[] args) throws Exception
{
final char[][] maze = loadMaze();
printMaze(maze);
final Coordinate start = readStartingCoordinate(maze);
final LinkedList path = new LinkedList();
path.push(start);
solveMaze(maze, path);
if (solveMaze(maze, path)) {
cleanMaze(maze, path, start);
printMaze(maze);
System.out.println("I am free");
}
else {
printMaze(maze);
System.out.println("Help, I am trapped");
}
}
static {
Main.keyboard = new Scanner(System.in);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
class Coordinate
{
private int row;
private int column;
public Coordinate(final int row, final int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return this.row;
}
public int getColumn() {
return this.column;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
class LinkedList
{
private Node head;
public LinkedList() {
this.head = null;
}
public void push(final Coordinate value) {
final Node node = new Node(value);
node.next = this.head;
this.head = node;
}
public Coordinate peek() {
return this.head.value;
}
public Coordinate pop() {
final Coordinate coordinate = this.head.value;
this.head = this.head.next;
return coordinate;
}
public boolean isEmpty() {
return this.head == null;
}
private class Node
{
public Coordinate value;
public Node next;
public Node(final Coordinate value) {
this.value = value;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Output :
Typing 20*20 matrix was quite lengthy, so I used 4*4 matrix :