How its gonna look like with recursion, not with vector #include #define MAX 5 using namespace std; // Function returns true if the // move taken is valid else // it will return false. bool isSafe(int row, int col, int m[][MAX], int n, bool visited[][MAX]) { if (row == -1 || row == n || col == -1 || col == n || visited[row][col] || m[row][col] == 0) return false; return true; } // Function to print all the possible // paths from (0, 0) to (n-1, n-1). void printPathUtil(int row, int col, int m[][MAX], int n, string& path, vector& possiblePaths, bool visited[][MAX]) { // This will check the initial point // (i.e. (0, 0)) to start the paths. if (row == -1 || row == n || col == -1 || col == n || visited[row][col] || m[row][col] == 0) return; // If reach the last cell (n-1, n-1) // then store the path and return if (row == n - 1 && col == n - 1) { possiblePaths.push_back(path); return; } // Mark the cell as visited visited[row][col] = true; // Try for all the 4 directions (down, left, // right, up) in the given order to get the // paths in lexicographical order // Check if downward move is valid if (isSafe(row + 1, col, m, n, visited)) { path.push_back('D'); printPathUtil(row + 1, col, m, n, path, possiblePaths, visited); path.pop_back(); } // Check if the left move is valid if (isSafe(row, col - 1, m, n, visited)) { path.push_back('L'); printPathUtil(row, col - 1, m, n, path, possiblePaths, visited); path.pop_back(); } // Check if the right move is valid if (isSafe(row, col + 1, m, n, visited)) { path.push_back('R'); printPathUtil(row, col + 1, m, n, path, possiblePaths, visited); path.pop_back(); } // Check if the upper move is valid if (isSafe(row - 1, col, m, n, visited)) { path.push_back('U'); printPathUtil(row - 1, col, m, n, path, possiblePaths, visited); path.pop_back(); } // Mark the cell as unvisited for // other possible paths visited[row][col] = false; } // Function to store and print // all the valid paths void printPath(int m[MAX][MAX], int n) { // vector to store all the possible paths vector possiblePaths; string path; bool visited[n][MAX]; memset(visited, false, sizeof(visited)); // Call the utility function to // find the valid paths printPathUtil(0, 0, m, n, path, possiblePaths, visited); // Print all possible paths for (int i = 0; i < possiblePaths.size(); i++) cout << possiblePaths[i] << " "; } // Driver code int main() { int m[MAX][MAX] = { { 1, 1, 1, 1, 1 }, { 1, 1, 1, 0, 1 }, { 0, 1, 1, 0, 1 }, { 0, 1, 0, 0, 1 }, { 0, 1, 1, 1, 1 } }; int n = sizeof(m) / sizeof(m[0]); printPath(m, n); return 0; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How its gonna look like with recursion, not with vector

#include <bits/stdc++.h>
#define MAX 5
using namespace std;

// Function returns true if the
// move taken is valid else
// it will return false.
bool isSafe(int row, int col, int m[][MAX],
int n, bool visited[][MAX])
{
if (row == -1 || row == n || col == -1 ||
col == n || visited[row][col]
|| m[row][col] == 0)
return false;

return true;
}

// Function to print all the possible
// paths from (0, 0) to (n-1, n-1).
void printPathUtil(int row, int col, int m[][MAX],
int n, string& path, vector<string>&
possiblePaths, bool visited[][MAX])
{
// This will check the initial point
// (i.e. (0, 0)) to start the paths.
if (row == -1 || row == n || col == -1
|| col == n || visited[row][col]
|| m[row][col] == 0)
return;

// If reach the last cell (n-1, n-1)
// then store the path and return
if (row == n - 1 && col == n - 1) {
possiblePaths.push_back(path);
return;
}

// Mark the cell as visited
visited[row][col] = true;

// Try for all the 4 directions (down, left,
// right, up) in the given order to get the
// paths in lexicographical order

// Check if downward move is valid
if (isSafe(row + 1, col, m, n, visited))
{
path.push_back('D');
printPathUtil(row + 1, col, m, n,
path, possiblePaths, visited);
path.pop_back();
}

// Check if the left move is valid
if (isSafe(row, col - 1, m, n, visited))
{
path.push_back('L');
printPathUtil(row, col - 1, m, n,
path, possiblePaths, visited);
path.pop_back();
}

// Check if the right move is valid
if (isSafe(row, col + 1, m, n, visited))
{
path.push_back('R');
printPathUtil(row, col + 1, m, n,
path, possiblePaths, visited);
path.pop_back();
}

// Check if the upper move is valid
if (isSafe(row - 1, col, m, n, visited))
{
path.push_back('U');
printPathUtil(row - 1, col, m, n,
path, possiblePaths, visited);
path.pop_back();
}

// Mark the cell as unvisited for
// other possible paths
visited[row][col] = false;
}

// Function to store and print
// all the valid paths
void printPath(int m[MAX][MAX], int n)
{
// vector to store all the possible paths
vector<string> possiblePaths;
string path;
bool visited[n][MAX];
memset(visited, false, sizeof(visited));

// Call the utility function to
// find the valid paths
printPathUtil(0, 0, m, n, path,
possiblePaths, visited);

// Print all possible paths
for (int i = 0; i < possiblePaths.size(); i++)
cout << possiblePaths[i] << " ";
}

// Driver code
int main()
{
int m[MAX][MAX] = {
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 1, 0, 1 },
{ 0, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1 }
};

int n = sizeof(m) / sizeof(m[0]);
printPath(m, n);

return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY