Question

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 only move 1 step in one of 4 directions. Valid moves are: Ø Go North: (x,y) -> (x,y-1) Ø Go East: (x,y) -> (x+1,y) Ø Go South: (x,y) -> (x,y+1) Ø Go West: (x,y) -> (x-1,y) Note that positions are specified in zero-based coordinates (i.e., 0...size-1, where size is the size of the maze in the corresponding dimension). The robot can only move to positions without obstacles and must stay within the maze. The robot should search for a path from the starting position to the goal position (a solution path) until it finds one or until it exhausts all possibilities. In addition, it should mark the path it finds (if any) in the maze. To make this problem more concrete, let's consider a maze represented by a matrix of characters. An example 6x6 input maze is: S##### .....# #.#### #.#### 'S' - start position (here, x=0, y=0) ...#.G 'G' - goal (here, x=5, y=4) A path in the maze can be marked by the '+' symbol... A path refers to either a partial path, marked while the robot is still searching: +##### ++++.# (i.e., one that may or may #.#### #.#### not lead to a solution). Or, ...#.G a solution path: ##...# S##### which ++...# #+#### leads from #+#### .++#+G ##+++# ##...# '.' - where the robot can move (open positions) '#' - obstacles (blocked positions)

Thank you, I will upvote your answer if it works and is correct

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

#include <iostream>
#include <vector>
using namespace std;

#define M 5 //Number of rows
#define N 6 //Number of columns

// Function to check if (i, j) is valid matrix coordinate
int flag = 0;
vector<pair<int,int> > path;
vector<vector<int> > vis(M,vector<int>(N,0));

bool isvalid(int i, int j,char mat[][N])
{
   return (i >= 0 && i < M && j >= 0 && j < N && vis[i][j]==0 && mat[i][j]!='#');
}

//// Function to print the route taken
//void printPath(vector<int> const &path, int last)
//{
//   for (int i : path)
//       cout << i << " - ";
//   cout << last << endl;
//}

void findPaths(char mat[][N], int i, int j)
{
   // if we have reached the last cell, print the route
   if (mat[i][j] == 'G')
   {  
       flag=1;
       return;
   }
  
   // include current cell in path
   vis[i][j]=1;
   path.push_back({i,j});  

   // move right
   if (isvalid(i, j + 1,mat))
       findPaths(mat, i, j + 1);
   if(flag)
       return;

   // move down
   if (isvalid(i + 1, j,mat))
       findPaths(mat,i + 1, j);
   if(flag)
       return;
      
   if (isvalid(i, j - 1,mat))
       findPaths(mat,i + 1, j);
   if(flag)
       return;
      
   if (isvalid(i - 1, j,mat))
       findPaths(mat,i + 1, j);
   if(flag)
       return;

   // remove current cell from the path
   path.pop_back();
   vis[i][j]=0;
}

// main function
int main()
{
  
  
  
   char mat[M][N];
   int sx,sy;
   cout<<"Enter the elements (seperated by space) : ";
   for(int i=0;i<M;i++){
       for(int j=0;j<N;j++){
           cin>>mat[i][j];
           if(mat[i][j]=='S'){
               sx=i;sy=j;
           }
       }
      
}

   findPaths(mat, sx, sy);
   if(flag==0)
       cout<<"No path found"<<endl;
   for(int i=0;i<path.size();i++){
       int x = path[i].first;
       int y = path[i].second;
       mat[x][y]='+';
   }
   mat[sx][sy]='S';
  
   for(int i=0;i<M;i++){
       for(int j=0;j<N;j++)
           cout<<mat[i][j]<<" ";
       cout<<endl;
   }

   return 0;
}

INPUT :

S # #

. # #

. . G

OUTPUT :

S # #

+ # #

+ + G

// IF NO PATH IS PRESENT OUTPUT IS : No path found

//If you got the answer, please upvote :)

Add a comment
Know the answer?
Add Answer to:
URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this...
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
  • The Problem A robot is asked to navigate a maze. It is placed at a certain...

    The Problem 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 only move 1 step in one of 4 directions. Valid moves are: ● Go North: (x,y) -> (x,y-1) ●...

  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • 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 # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . #...

  • 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...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

  • In this question, you will test, using a backtracking algorithm, if a mouse can escape from...

    In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...

  • For this program you'll write a program that uses a recursive function. This program plays a simp...

    C++ For this program you'll write a program that uses a recursive function. This program plays a simple token-taking game. The rules are simple 1. 2. You start the game with exactly 13 tokens. On each turn, you may do one of two things You may ask for exactly 25 more tokens; or IF the number of tokens you have is an even number, you may give back exactly half of the tokens you have a. b. 3. The object...

  • 2 Super Mario Run A Mario World M consists of a k xk grid. Each field...

    2 Super Mario Run A Mario World M consists of a k xk grid. Each field in the grid is either empty or brick. Two empty fields are marked as start and goal (see Fig. 2(a)). The goal of the game is to move the player, called Mario, from the start field to the goal field. When Mario is in field (x,y) he has the following options: Forward Mario moves to the field (x + 1,y). This move is possible...

  • 2 Super Mario Run A Mario World M consists of a k xk grid. Each field...

    2 Super Mario Run A Mario World M consists of a k xk grid. Each field in the grid is either empty or brick. Two empty fields are marked as start and goal (see Fig. 2(a)). The goal of the game is to move the player, called Mario, from the start field to the goal field. When Mario is in field (x,y) he has the following options: Forward Mario moves to the field (x+1, y). This move is possible if...

  • Instructions Write a program in Java that implements the A* algorithm to find a path from...

    Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...

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