HackerEarth Matrix Symmetry problem solution

In this Leetcode Matrix Symmetry problem solution, You are given a square matrix of size n. Rows are indexed 1 to n from top to bottom and columns are indexed 1 to n from left to right. The matrix consists of only '*' and '.'. You need to check whether the matrix is symmetric or not. if it is, check it is symmetric about the vertical axis or horizontal axis, or both.

A matrix is said to be symmetric about the horizontal axis if the 1st row is identical to the nth row, the 2nd is identical to (n - 1)th row, and so on...

A matrix is said to be symmetric about the vertical axis if the 1st column is identical to the nth column, 2nd identical to (n - 1)th and so on for all columns.


HackerEarth Matrix Symmetry problem solution


HackerEarth Matrix Symmetry problem solution.

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<fstream>
#include<cstdlib>
#include<cassert>
#include<vector>
#include<algorithm>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<ctime>
#define LL long long
#define ULL unsigned long long
#define F firs
#define S second
#define pb push_back
#define FOR(i,lb,ub) for(i=lb;i<=ub;i++)
#define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
#define FORS(it,v) for(it=v.begin();it!=v.end();it++)
#define st_clk double st=clock();
#define end_clk double en=clock();
#define show_time cout<<"\tTIME="<<(en-st)/CLOCKS_PER_SEC<<endl;
#define sc(n) scanf("%d",&n)
#define scst(n) scanf("%s",n)
#define f_in(st) freopen(st,"r",stdin);
#define f_out(st) freopen(st,"w",stdout);
LL gcd(LL a, LL b) { return b?gcd(b,a%b):a; }
using namespace std;
#ifndef ONLINE_JUDGE
inline int getchar_unlocked() { return getchar(); }
#endif
template <class T>
inline void r_f(T &p)
{
char c;
int neg=0;
while ((c=getchar_unlocked()) < 48 || c > 57)
if (c==45)
neg=1;
p=c-48;
while ((c=getchar_unlocked()) >= 48 && c <= 57) p=p*10+c-48;
if (neg) p*=-1;
}
int main()
{
#ifndef ONLINE_JUDGE
int no;
cin>>no;
char istr[20],ostr[20];
sprintf(istr,"input-%d.txt",no);
sprintf(ostr,"out-%d.txt",no);
f_in(istr);
f_out(ostr);
#endif
int t,i,j;
cin>>t;
while (t--)
{
int n,hori=1,vert=1;
cin>>n;
char mat[50][50];
FOR(i,0,n-1)
cin>>mat[i];
FOR(i,0,n-1)
FOR(j,0,n-1)
{
if (mat[i][j]!=mat[n-1-i][j])
hori=0;
if (mat[i][j]!=mat[i][n-1-j])
vert=0;
}
if (vert && hori)
cout<<"BOTH\n";
else if (vert)
cout<<"VERTICAL\n";
else if (hori)
cout<<"HORIZONTAL\n";
else
cout<<"NO\n";
}
return 0;
}

Post a Comment

0 Comments