Question

In: Computer Science

PLEASE ANSWER IN C++ Given the starting point in a maze, you are to find and...

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>

Solutions

Expert Solution

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 :



Related Solutions

In a maze with starting and ending point. Genetic Algorithm can be used to find the...
In a maze with starting and ending point. Genetic Algorithm can be used to find the path through the maze. 1. What does each chromosome represent? 2. How to evaluate the chromosome (Fitness function)?
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that a player must traverse to win. 3. The maze must be text-based and adjustable from 5x5 to 20x20. • Player gets to choose size either as: 1) any size in the range from 5-20 by 5-20. 2) selects from 4 set size options [5x5,10x10,15x15, and 20x20] • the player can choose among different mazes. • You will need to figure out how to denote...
Find a point on a given line such that if it is joined to two given...
Find a point on a given line such that if it is joined to two given points on opposite sides of the line, then the angle formed by the connecting segment is bisected by the given line.
Write the code that will produce the given "after" result from the given "before" starting point...
Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown. Assume that the nodes have already been declared and initialized to match the "before" figure below. There may be more than one way to write the code, but do NOT change any existing node's data field value. Do not create any new nodes, you must just rearrange the existing nodes.   If a variable does not appear in...
PLEASE ANSWER THE LAST QUESTIONS (as many as you can starting with the last question) On...
PLEASE ANSWER THE LAST QUESTIONS (as many as you can starting with the last question) On January 1, 2016, the following information was drawn from the accounting records of Carter Company: cash of $400; land of $2,400; notes payable of $700; and common stock of $1,540. Required a. Determine the amount of retained earnings as of January 1, 2016. g. During 2016, Carter Company earned cash revenue of $660, paid cash expenses of $380, and paid a cash dividend of...
A8. For air with temperature and dew-point values given below in °C, find the LCL value...
A8. For air with temperature and dew-point values given below in °C, find the LCL value (km). T, Td T, Td a. 15, 12 g. 5, 4 b. 15, 10 h. 5, 0 c. 15, 8 i. 5, –5
Thumbs Up Will Be Given For Answer. Chapter Topic: The Organization of IB Please find an...
Thumbs Up Will Be Given For Answer. Chapter Topic: The Organization of IB Please find an INTERNATIONAL BUSINESS article to use. By doing so, YOU NEED to please visit websites such as  Reuters, Bloomberg, Wall Street Journal, etc, and put in the keywords "International Business" and the chapter topic "The Organization of IB​​​​​​​" on the website. You NEED to choose a relevant and interesting article that was made within the PAST COUPLE MONTHS that has to do with "International Business" and...
Develop and write a program in c/c++ that will find the first 100 primes (starting at...
Develop and write a program in c/c++ that will find the first 100 primes (starting at 1) and write them to a file named "primes.dat".
You find yourself in a maze walking and you reach a 3-way intersection. The roads have...
You find yourself in a maze walking and you reach a 3-way intersection. The roads have signs as follows: -on the left: “bad idea street” -in front of you: “don’t even try it road” -on the right: “you’re lost avenue” On each one of the roads there is a street vendor (hey, it’s Miami, they are everywhere!). You talk to the street vendors and this is what they tell you (after you buy some of their goods of course): -Street...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT