

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all palindromic paths from top left to bottom right in a matrix in C++
In this problem, we are given a matix which contains aplhabets (lowercase only) and we have to print all palidromic paths in the given matrix from top left to bottom right of the matrix.
Allowed moves in this problem are right and down. Diagonal moves are not allowed.
Let’s take an example to understand the problem −
Input: matrix[][] ={ {"xxxy", "yxxx", "xyyx"} Output: xxxxxx , xxxxxx , xyxxyx
Explaination
Lets see all valid moves from top left to bottom right using the position wrt to ith position.
i00 -> i01 -> i02 -> i03 -> i13 -> i23 = xxxyxx i00 -> i01 -> i11 -> i12 -> i13 -> i23 = xxxxxx . . . i00 -> i10 -> i20 -> i21 -> i22 -> i23 = xyxyyx
Out of all possible outcomes, we need only palindromic path as outcomes that are −
i00 -> i01 -> i11 -> i12 -> i13 -> i23 = xxxxxx i00 -> i01 -> i02 -> i12 -> i13 -> i23 = xxxxxx i00 -> i10 -> i11 -> i12 -> i22 -> i23 = xyxxyx
In the explanation itself, we have laid the base of the solution to the problem. We will find all paths from top-left to bottom-right and print all those which give results to palindromic path.
Example
The example below will illustrate the solution −
#include<iostream> using namespace std; #define N 4 int printPalindrome(string str){ int len = str.length() / 2; for (int i = 0; i < len; i++) { if (str[i] != str[str.length() - i - 1]) return 0; } cout<<str<<endl; } void findPath(string str, char a[][N], int i, int j, int m, int n) { if (j < m - 1 || i < n - 1) { if (i < n - 1) findPath(str + a[i][j], a, i + 1, j, m, n); if (j < m - 1) findPath(str + a[i][j], a, i, j + 1, m, n); } else { str = str + a[n - 1][m - 1]; printPalindrome(str) ; } } int main() { char matrix[][N] = { { 'x', 'y', 'x', 'y' }, { 'y', 'x', 'x', 'y' }, { 'y', 'x', 'y', 'x' } }; string str = ""; cout<<"Palimdromic path are : "; findPath(str, matrix, 0, 0, 4, 3); return 0; }
Output
Palimdromic path are : xyxxyx xyxxyx xyxxyx xyxxyx xyxxyx xyxxyx xyxxyx xyxxyx
- Related Questions & Answers
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Maximum points from top left of matrix to bottom right and return back in C++
- Print a matrix in alternate manner (left to right then right to left) in C++
- How to display the JList items from top to bottom and left to right in Java?
- Set the left, right, top and bottom padding of an element
- Print all leaf nodes of a binary tree from right to left in C++
- How to add a straight line to a plot in R starting from bottom left and ending at top right?
- Maximum sum path in a matrix from top to bottom in C++
- Print all paths from a given source to a destination in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Program to count number of walls required to partition top-left and bottom-right cells in Python
Advertisements