Question

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>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The C++ code of the above question is given as follows in which we have used dfs and visited arr of Boolean type to maintain the visited path to eliminate the stack over flow and repeatitive calls :

As per the question the input and output formats were not properly described and were written in confusion, so as far as I get the question and done it. Besides the main logic still remains the same.

//C++ code

#include <iostream>
using namespace std;

bool dfs(string arr[],bool visited[20][20],int i,int j){
  
if(i<0 || j<0 || i>=20 || j>=20 || visited[i][j]==1 || arr[i][j]=='1'){
return 0;
}
  
if(arr[i][j]=='E'){
return 1;
}
  
visited[i][j]=1;
  
bool s = dfs(arr,visited,i+1,j)
|| dfs(arr,visited,i-1,j)
|| dfs(arr,visited,i,j-1)
|| dfs(arr,visited,i,j+1);
  
if(!s){

visited[i][j] = 0;
}
  
return s;
  
}

int main() {
   string arr[20]={"E0001110000000100100",
"11100011101110001111",
"11111000101000111000",
"00001110101100010010",
"01011000101111000110",
"00001110000110011110",
"11011100110110111000",
"00011110110111111101",
"01011011110110100001",
"01000000000110110111",
"11011011010010000000",
"01010010011000101011",
"01111000101110101110",
"00001110000111011001",
"01101011101101000011",
"11000110100111011010",
"01110000100100110011",
"11010111110110000000",
"01110100011000111110",
"00011001000011100010"};

bool visited[20][20];
  
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
visited[i][j]=0;
}
}
  
int si;
int sj;
  
cin>>si;
cin>>sj;
  
bool ans = dfs(arr,visited,si-1,sj-1);
  
if(ans==1){
cout<<si<<" "<<sj<<endl;
cout<<"I am free."<<endl;
}else{
cout<<"I am trapped"<<endl;
}
  
if(ans){
  
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
if(visited[i][j]==1){
cout<<"+";
}
else{
cout<<arr[i][j];
}
}
cout<<endl;
}
}
  
   return 0;
}

For the input of
si=2

sj=6

the output is:

2 6
I am free.
E+++1110000000100100
111+++11101110001111
11111000101000111000
00001110101100010010
01011000101111000110
00001110000110011110
11011100110110111000
00011110110111111101
01011011110110100001
01000000000110110111
11011011010010000000
01010010011000101011
01111000101110101110
00001110000111011001
01101011101101000011
11000110100111011010
01110000100100110011
11010111110110000000
01110100011000111110
00011001000011100010

Add a comment
Know the answer?
Add Answer to:
PLEASE ANSWER IN C++! Given the starting point in a maze, you are to find and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you......

    Need help!! Java Eclipse Please provide the screenshot of output of code as well. thank you... PROGRAM 1 –Linear Data Structure Implementation (Due date: March 5th, 2019, 5% Grade Points) Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 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...

  • For this homework you will be making a maze solver. Your program will take in from...

    For this homework you will be making a maze solver. Your program will take in from a file 2 things. The size of the square maze, and the maze itself. The maze will consists of numbers between 0 and 3, where 0 is the start of the maze, 1 is an open path, 3 is a wall, and 2 is the end of the maze. For example a 6x6 maze could be represented by the following file, there will be...

  • relevant comments and indentations should be included. Q1: (Recursive Maze Traversal) (75 points) The following grid...

    relevant comments and indentations should be included. Q1: (Recursive Maze Traversal) (75 points) The following grid is a double-subscripted array representation of a maze The # symbols represent the walls of the maze, and the periods (.) represent squares in the possible paths through the maze. There's a simple algorithm for walking through a maze that guarantees finding the exit (assuming there's an exit). If there's not an exit, you'll arrive at the starting location again. Place your right hand...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

    ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file that contains the maze. Read it into a two-dimensional array Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters Don’t forget to read the newline character at the end of each line Print the maze to the screen from your array You should include a ‘*’ in your maze to indicate where the...

  • Java maze problem: Given a maze represented by a 2-dimensional array with 0’s (wall) and 1’s...

    Java maze problem: Given a maze represented by a 2-dimensional array with 0’s (wall) and 1’s (open path), the goal is to find the path from a given initial location (rowIni, colIni) to a final location (rown, coln) using recursive backtracking. A move can be made to a location only if there is 1 in the neighboring coordinate. A position that is part of path should be marked with the character +. When a path from initial location to final...

  • Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone...

    Hey everyone, I have a programing projest from teacher, but I don't understand what is says.....Anyone can help? (Maze Traversal) The following grid of #s and dot ( . ) is a double-subscripted array representation of a maze. # # # # # # # # # # # # # .   .   .   # .   .   .   .   .   .   # .   .   # .   # .   # # # # .   # # # # .   # .  ...

  • URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this...

    URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this maze problem: (No classes please!!!!) A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can...

  • This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#)...

    This lab will use 2D arrays, recursive algorithms, and logical thinking. The following grid of hashes(#) and dots(.) is a 2D array representation of a maze # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . #...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignment you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT