Header Ads Widget

Matrix Symmetry

problem:-

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 form left to right. Matrix consists of only '*' and '.'. You need to check whether matrix is symmetric or not. if it is, check it is symmetric about vertical axis or horizontal axis or both.
A matrix is said to be symmetric about horizontal axis if 1st row is identical to nth row, 2nd is identical to (n1)th row and so on...
A matrix is said to be symmetric about vertical axis if 1st column is identical to nth column, 2nd identical to (n1)th and so on for all columns.
INPUT :
First line contains t,the number of test cases. First line of each test case contains n the size of matrix. Each of next n lines contain n characters.
OUTPUT:
Output t lines, answer for each test case. Print "HORIZONTAL" if symmetric about horizontal axis. Print "VERTICAL" if symmetric about vertical axis. Print "BOTH" if symmetric about both axes. print "NO" if it is not symmetric.
Constraints :
1<t500
1<n<50
SAMPLE INPUT
 
3
4
*.*.
.*.*
*.*.
.*.*
3
.*.
*.*
.*.
3
..*
**.
..*
SAMPLE OUTPUT
 
NO
BOTH
HORIZONTAL
Time Limit:2.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:

solution:-

#include<stdio.h>
#include<string.h>
void main()
{
      char s[50][50];
     int t,n,r1,flag2=0,flag1=0,k,j;
     scanf("%d",&t);
     for(int i=1;i<=t;i++)
     {
         scanf("%d",&n);
         for( j=0;j<n;j++)
         {
             scanf("%s",s[j]);
         }
         for( j=0;j<n/2;j++)
         {
            r1=strcmp(s[j],s[n-j-1]);
            if(r1!=0)
             break;
         }
         if(j==n/2)
         flag1=1;
         for( k=0;k<n;k++)
         {
            for(j=0;j<n/2;j++)
            {
                if(s[k][j]!=s[k][n-j-1])
                 break;
            }
            if(j!=n/2)
            {
             break;
            }
        }
        if(k==n)
         flag2=1;
        if(flag1==1&&flag2==1)
         printf("BOTH\n");
        else
        {
            if(flag1==1)
             printf("HORIZONTAL\n");
            if(flag2==1)
             printf("VERTICAL\n");
        }
        if(flag1==0&&flag2==0)
         printf("NO\n");
        flag1=0;
        flag2=0;

    }
}

Post a Comment

0 Comments