# 【題解】ZeroJudge e513: 11349 – Symmetric Matrix

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e513
【解題想法】

• 依題意，對稱矩陣
• 所有元素都必須是“非負”
• 相對於該矩陣的中心對稱 (下圖例)
• (Line-12) 矩陣內的元素範圍為 -232 <= Mij <= 232，需宣告成long long
```#include <iostream>
using namespace std;

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T, n;
char ch;
cin >> T;
for (int Case = 1; Case <= T; Case++){
cin >> ch >> ch >> n;
long long a[n][n];
bool flag = true;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> a[i][j];
if (a[i][j] < 0) flag = false;
}
}
cout << "Test #" << Case << ": ";
if (!flag){
cout << "Non-symmetric.\n";
continue;
}

for (int i = 0; i <= n/2; i++){
for (int j = 0; j < n-i; j++){
if (a[i][j] != a[n-1-i][n-1-j]){
flag = false;
break;
}
}
}
if (flag) cout << "Symmetric.\n";
else cout << "Non-symmetric.\n";
}
return 0;
}
```

Python code (credit: Amy Chou)

```T = int(input())
for Case in range(T):
n = int(input().split(" = ")[1])
M = list()
for i in range(n):
M.append(list(map(int, input().split())))

is_Symmetric = True
center = n // 2
for i in range(n):
for j in range(i+1):
if M[i][j] < 0 or M[n - 1 - i][n - 1 - j] < 0:
is_Symmetric = False
break
if M[i][j] != M[n - 1 - i][n - 1 - j]:
is_Symmetric = False
break
if is_Symmetric is False:
break

if is_Symmetric:
print(f"Test #{Case+1}: Symmetric.")
else:
print(f"Test #{Case+1}: Non-symmetric.")
```