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 n ; th row, 2nd is identical to (n-1); 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 (n-1) ; 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 < t ≤ 500
1 < n < 50
SAMPLE INPUT
3
4
..
....
..
3
..
.
..
3
..*
*.
..
SAMPLE OUTPUT
NO
BOTH
HORIZONTAL
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read input and for each test case determine symmetry. Constraints: t up to 500, n up to 49 (since n < 50, and 1 < n means n >= 2? Actually says 1 < n < 50, so n from 2 to 49). So we can simply check.
For each test case, we have n lines each of length n. Check horizontal symmetry: For each row i (0-indexed), compare with row n-1-i. They must be identical. Since if n is even, all rows paired; if odd, middle row is okay by itself.
Similarly vertical symmetry: For each column j, compare with column n-1-j. This can be done by checking for each row, character at j equals character at n-1-j. Equivalent to each row being palindrome.
If both conditions true, output BOTH; else if horizontal true, output HORIZONTAL; else if vertical true, output VERTICAL; else NO.
Edge cases: Must be exactly "HORIZONTAL", "VERTICAL", "BOTH", "NO". Sample: first matrix: 4x4, pattern? Let's see: lines:
..
....
..
Check horizontal: row0 vs row3: .. vs ..? Different, so no horizontal. Vertical: check each row palindrome? row0: * . * . -> positions: 0 vs 3: * vs .? No, so vertical fails. So NO.
Second: 3x3:
..
.
..
Horizontal: row0 vs row2: .. vs .. -> same; row1 middle ok. So horizontal true. Vertical: each row palindrome? row0: . * . -> palindrome? positions 0 vs 2: . vs . yes; row1: * . * -> yes; row2: . * . -> yes; so vertical true. So BOTH.
Third: 3x3:
..*
.
..
Horizontal: row0 vs row2: .. vs ..* same; so horizontal true. Vertical: row0: . . * -> 0 vs2: . vs *? No, so vertical false. So HORIZONTAL.
Matches output.
Thus implementation straightforward.
Note: Input lines may contain characters without spaces. Use input().strip().
Potential pitfalls: n can be up to 49 but small.
We'll implement a solve() function.
Complexity O(t * n^2) which is fine.
Now produce answer in Python.
We need to parse t test cases.
Implementation details:
def is_horizontal(matrix, n):
for i in range(n//2):
if matrix[i] != matrix[n-1-i]:
return False
return True
def is_vertical(matrix, n):
for i in range(n):
row = matrix[i]
for j in range(n//2):
if row[j] != row[n-1-j]:
return False
return True
Then for each test case, read n, read n lines, strip newline, store as strings.
Then check.
Edge: n lines may have trailing newline; use .strip().
Now produce final answer.
We'll include main guard.
We'll write:
import sys
def main():
data = sys.stdin.read().strip().splitlines()
if not data: return
t = int(data[0].strip())
idx = 1
out_lines = []
for _ in range(t):
# skip empty lines?
while idx < len(data) and data[idx].strip() == '':
idx += 1
n = int(data[idx].strip())
idx += 1
matrix = []
for i in range(n):
matrix.append(data[idx].strip())
idx += 1
# Check symmetry
h = is_horizontal(matrix, n)
v = is_vertical(matrix, n)
if h and v:
out_lines.append("BOTH")
elif h:
out_lines.append("HORIZONTAL")
elif v:
out_lines.append("VERTICAL")
else:
out_lines.append("NO")
sys.stdout.write("\n".join(out_lines))
Define functions before.
Alternatively, we can process line by line.
That's it.
Now produce final answer. ```python
import sys
def is_horizontal(matrix, n):
"""Check symmetry about the horizontal axis."""
for i in range(n // 2):
if matrix[i] != matrix[n - 1 - i]:
return False
return True
def is_vertical(matrix, n):
"""Check symmetry about the vertical axis."""
for row in matrix:
for j in range(n // 2):
if row[j] != row[n - 1 - j]:
return False
return True
def solve() -> None:
data = sys.stdin.read().strip().splitlines()
if not data:
return
t = int(data[0])
idx = 1
out = []
for _ in range(t):
# skip possible empty lines
while idx < len(data) and data[idx].strip() == '':
idx += 1
n = int(data[idx])
idx += 1
matrix = []
for _ in range(n):
matrix.append(data[idx].rstrip())
idx += 1
code
h = is_horizontal(matrix, n)
v = is_vertical(matrix, n)
if h and v:
out.append("BOTH")
elif h:
out.append("HORIZONTAL")
elif v:
out.append("VERTICAL")
else:
out.append("NO")
sys.stdout.write("\n".join(out))