UVa 11349 – Symmetric Matrix
You're given a square matrix M.
The elements of matrix M are denoted as M[i][j]: {0 < i < N, 0 < j < N}.
In this problem, you must determine whether the given matrix is symmetric.
Definition: A symmetric matrix has all non-negative elements and is symmetric concerning the center of the matrix.
Any other matrix is considered non-symmetric.
What you need to do is determine whether this matrix is symmetric.
The elements within the matrix range from
−2^32 to 2^32, and 0 < N ≤ 100.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input contains a number T (T ≤ 300), indicating the number of test cases. The first line of each test case contains an integer N, indicating the dimension of the square matrix. The next N lines contain the elements of the matrix. Each of the N lines contains N numbers, where the j-th number on the i-th line represents M[i][j]. | For each test case, output a line in the format "Test #t: S", where t is the test case number and S is the answer string.
If the input matrix is symmetric, S= "Symmetric". If the input matrix is not symmetric, S= "Non-symmetric". |
2 N = 3 5 1 3 2 0 2 3 1 5 N = 3 5 1 3 2 0 2 0 1 5 | Test #1: Symmetric. Test #2: Non-symmetric. |
Thought Process
In this question, Long Long Int should be used to store the data in the matrix. When receiving the data, if a number less than 0 is encountered, you can set a boolean value to true, and then there's no need to perform further checks.
You can collect the data from the matrix into a one-dimensional array and then check if this one-dimensional array is a palindrome. This way, you can determine if the matrix is symmetric.
Sample Code-ZeroJudge E513: Symmetric Matrix
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
char a, b;
int M;
cin >> a >> b >> M;
vector<long long int>num;
bool negative = false;
for (int j = 0; j<M; j++)
{
for (int k = 0; k<M; k++)
{
long long int tmp;
cin >> tmp;
num.push_back(tmp);
if (tmp < 0) negative = true;
}
}
if (!negative)
{
vector<long long int>tmp = num;
reverse(tmp.begin(), tmp.end());
if (tmp == num) cout << "Test #" << i+1 << ": Symmetric.\n";
else cout << "Test #" << i+1 << ": Non-symmetric.\n";
}
else cout << "Test #" << i+1 << ": Non-symmetric.\n";
}
}
//ZeroJudge E513
//Dr. SeanXD