1 December 2014

Quiz 59: Is the matrix symmetric

Problem:

Given a NxN Matrix(N is odd). Each block contains a letter from A-Z. Find whether matrix is symmetric with respect to central block. Central block for 3x3 matrix is (1,1), index starting from 0.

Input Format: 

N
Next N lines containing content of each row.

Output Format: 

YES/NO- whether matrix is symetric or not


Constraints: 

N is always odd

Sample Input

5
ABFWW
TTTOP
ASDSA
POTTT
WWFBA


Sample Output:

YES

Explanations:

It is symmetric wrt to D(2,2) , example (0,0)=(4,4)=A


Solution:

chomp($n=<STDIN>);
$str;
for($i=0;$i<$n;$i++)
{
chomp($tmp=<STDIN>);
$str=$str.$tmp;
}
if($str eq reverse($str))
{
print "YES";
}
else
{
print "NO";
}




Tips:

No need to make N arrays and then compare, use string

1 comment:

  1. I couldn't refrain from commenting. Exceptionally well written!

    Also visit my blog; CT limo

    ReplyDelete