C++ code to check pattern is center-symmetrical or not

C++Server Side ProgrammingProgramming

Suppose we have a 3 x 3 matrix with 'X' and '.'. We have to check whether the pattern is centersymmetrical or not. (More on center symmetry − http://en.wikipedia.org/wiki/Central_symmetry)


So, if the input is like

XX.
...
.XX

then the output will be True.

Steps

To solve this, we will follow these steps −

if M[0, 0] is same as M[2, 2] and M[0, 1] is same as M[2, 1] and M[0, 2] is same as M[2, 0] and M[1, 0] is same as M[1, 2], then:
   return true
Otherwise
   return false

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
bool solve(vector<vector<char>> M){
   if (M[0][0] == M[2][2] && M[0][1] == M[2][1] && M[0][2] == M[2][0] && M[1][0] == M[1][2])
      return true;
   else
      return false;
}
int main(){
   vector<vector<char>> matrix = { { 'X', 'X', '.' }, { '.', '.', '.' }, { '.', 'X', 'X' } };
   cout << solve(matrix) << endl;
}

Input

{ { 'X', 'X', '.' }, { '.', '.', '.' }, { '.', 'X', 'X' } }

Output

1
raja
Updated on 30-Mar-2022 12:59:02

Advertisements