
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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?
- Maximum sum path in a matrix from top to bottom in C++
- Print all leaf nodes of a binary tree from right to left 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
- How to add a straight line to a plot in R starting from bottom left and ending at top right?
- A current-carrying conductor is held in exactly vertical direction. In order to produce a clockwise magnetic field around the conductor, the current should passed in the conductor:(a) from top towards bottom (b) from left towards right (c) from bottom towards top (d) from right towards left
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Print all paths from a given source to a destination using BFS in C++
- Set the left, right, top and bottom padding of an element

Advertisements