
- 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
C++ Program to find out if a palindromic matrix can be made from a given matrix
Suppose, we are given a matrix with dimensions h x w. The matrix contains English letters. We have to create another matrix that will contain palindromic rows and columns, i.e. each row and column would be palindromes. To do that, any arrangement of rows and columns can be done from the given matrix; but no element can be changed, i.e. an 'a' cannot be changed to a 'b'. If that is possible to make a palindromic matrix from the given matrix, we return true; or otherwise, we return false.
So, if the input is like h = 4, w = 4, mat = {"xxyy", "xyxx", "yxxy", "xyyy"}, then the output will be true.
Steps
To solve this, we will follow these steps −
Define one map mp Define an array count of size 4. for initialize i := 0, when i < h, update (increase i by 1), do: for initialize j := 0, when j < w, update (increase j by 1), do: (increase tp[mat[i, j]] by 1) for each value val in tp, do: increase count[second value of val mod 4] by 1 check := true if h mod 2 is same as 0 and w mod 2 is same as 0, then: if count[1] + count[2] + count[3] > 0, then: check := false otherwise when h mod 2 is same as 1 and w mod 2 is same as 1, then: if count[1] + count[3] > 1, then: check := false otherwise when count[2] > h / 2 + w / 2, then: check := false Otherwise if count[1] + count[3] > 0, then: check := false otherwise when h mod 2 is same as 1 and count[2] > w / 2, then: check := false otherwise when w mod 2 is same as 1 and count[2] > h / 2, then: check := false return check
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; bool solve(int h, int w, vector<string> mat){ map<char, int> tp; vector<int> count(4); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) tp[mat[i][j]]++; } for (auto val : tp) count[val.second % 4]++; bool check = true; if (h % 2 == 0 && w % 2 == 0) { if (count[1] + count[2] + count[3] > 0) check = false; } else if (h % 2 == 1 && w % 2 == 1) { if (count[1]+count[3] > 1) check = false; else if (count[2] > h / 2 + w / 2) check = false; } else { if (count[1] + count[3] > 0) check = false; else if (h % 2 == 1 && count[2] > w / 2) check = false; else if (w % 2 == 1 && count[2] > h / 2) check = false; } return check; } int main() { int h = 4, w = 4; vector<string> mat = {"xxyy", "xyxx", "yxxy", "xyyy"}; cout<< solve(h, w, mat); return 0; }
Input
4, 4, {"xxyy", "xyxx", "yxxy", "xyyy"}
Output
1
- Related Articles
- Java Program To Determine If a Given Matrix is a Sparse Matrix
- Golang Program To Determine If a Given Matrix is a Sparse Matrix
- Python Program to find out the determinant of a given special matrix
- Swift program to check if a given square matrix is an Identity Matrix
- Program to find nth smallest number from a given matrix in Python
- Program to find maximum amount of coin we can collect from a given matrix in Python
- Program to convert given Matrix to a Diagonal Matrix in C++
- Program to find number of distinct island shapes from a given matrix in Python
- C++ Program to find out the maximum amount of money that can be made from selling cars
- Java Program To Find the Trace and Normal of a given Matrix
- Swift Program to Find the Trace and Normal of a given Matrix
- C++ Program To Find the Trace and Normal of a given Matrix
- Golang Program To Find the Trace and Normal of a given Matrix
- Program to check if a matrix is Binary matrix or not in C++
- Print all palindromic paths from top left to bottom right in a matrix in C++

Advertisements