- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check horizontal and vertical symmetry in binary matrix in C++
Suppose we have a binary matrix of order M x N. The task is to check whether the matrix is horizontally symmetric, vertically symmetric or both. One matrix is said to be horizontally symmetric if the ith row is the same as the (M – i)th row, this is said to be vertically symmetric if the jth column is the same as the (N – j)th column. Suppose the input matrices are like below −
0 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
This is Horizontally symmetric.
1 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
This is Vertically symmetric.
1 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
This is Horizontally as well as vertically symmetric.
We will solve this problem using two phases. At first, we will check whether the matrix is horizontally symmetric or not, of so, then set one Boolean variable horizontal_flag = true, otherwise false, if the matrix is vertically symmetric, then set vartical_flag = true, otherwise false. Of both are true then the answer will be totally symmetric.
Example
#include <iostream> #define MAX 20 using namespace std; void checkSymmetryLevel(int arr[][MAX], int N, int M) { bool horizontal_flag = true, vartical_flag = true; for (int i = 0, k = N - 1; i < N / 2; i++, k--) { for (int j = 0; j < M; j++) { if (arr[i][j] != arr[k][j]) { horizontal_flag = false; break; } } } for (int i = 0, k = M - 1; i < M / 2; i++, k--) { for (int j = 0; j < N; j++) { if (arr[i][j] != arr[k][j]) { vartical_flag = false; break; } } } if (!horizontal_flag && !vartical_flag) cout << "This is not symmetric from any point of view"; else if (horizontal_flag && !vartical_flag) cout << "This is not horizontally symmetric"; else if (vartical_flag && !horizontal_flag) cout << "This is vertically symmetric"; else cout << "This is symmetric in both direction"; } int main() { int mat[MAX][MAX] = { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } }; checkSymmetryLevel(mat, 3, 3); }
Output
This is symmetric in both direction
- Related Articles
- What letters of the English alphabet have reflectional symmetry $(i.e., symmetry related to mirror reflection)$ about.$(a)$. a vertical mirror$(b)$. a horizontal mirror$(c)$. both horizontal and vertical mirrors
- On a squared paper, sketch the following:(a) A triangle with a horizontal line of symmetry but no vertical line of symmetry.(b) A quadrilateral with both horizontal and vertical lines of symmetry.(c) A quadrilateral with a horizontal line of symmetry but no vertical line of symmetry.(d) A hexagon with exactly two lines of symmetry.(e) A hexagon with six lines of symmetry.(Hint : It will be helpful if you first draw the lines of symmetry and then complete the figures.)
- Program to check if a matrix is Binary matrix or not in C++
- CSS Central, Horizontal and Vertical Alignment
- Binary Tree Vertical Order Traversal in C++
- Horizontal and Vertical Center Alignment with Flexbox in CSS3
- Difference between vertical integration and horizontal integration
- Vertical and Horizontal Scrollbars on Tkinter Widget
- Vertical Text, with Horizontal Letters in CSS
- Find maximum vertical sum in binary tree in C++
- Program to check diagonal matrix and scalar matrix in C++
- Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts in C++
- Consider the letters of English alphabets, A to Z. List among them the letters which have(a) vertical lines of symmetry (like A)(b) horizontal lines of symmetry (like B)(c) no lines of symmetry (like Q)
- Shortest Path in Binary Matrix in C++
- Vertical Concatenation in Matrix in Python
