Description
Input an n * n matrix. If two different points A(x1, y1), B (x2, y2) satisfy x1 + x2 = n + 1, y1 + y2 = n + 1, and the number at positions A and B are the same, then we say the pair (A, B) is called a symmetric pair. Find the number of symmetric pairs. In this problem, both x and y start with 1.
Input
The first line has an integer n
Then n rows, with n integers between 0 and 100 per row
Output
An integer that represents the answer.
Examples
Input 1
5
3 3 3 4 1
2 0 0 3 1
0 3 1 4 1
3 4 3 3 1
1 0 3 3 1
Output 1
3
Examples Explanation
There are 3 pairs:
(1,2) and (5,4)
(1,3) and (5,3)
(1,5) and (5,1)
Constraint
1<=n<=500
WHAT I TRIED:
trings=list(input(“”))
answer_str=[]
for i in range(0, len(strings)):
if ord(min(strings))>40:
answer_str.append(min(strings))
strings.remove(min(strings))
else:
strings.remove(min(strings))
print(*answer_str, sep=“”)
A challenging problem in my homework, need help.
>Solution :
Here’s a solution:
#include <vector>
#include <iostream>
using namespace std;
int main(){
int n,a,r=0;
cin>>n;
vector<vector<int>>v;
for(int i=0;i<n;i++){
vector<int>v1;
for(int j=0;j<n;j++){cin>>a;v1.push_back(a);}
v.push_back(v1);
}
for(int y=0;y<n-1;y++)for(int x=n-y-2;x>=0;x--)if(v[y][x]==v[n+1-y][n+1-x])r++;
cout<<r<<endl;
}
UPDATE:
A bit of explanation: this code iterates vertically from 0 to n-2 (n-2 is just above the last row) by y, and from n-2-y (n-2 for the first iteration, then n-3 for the second), covering the entire triangle. On each iteration it checks whether the potential pair is equals to it.
UPDATE2:
Here is the code on Python:
n = int(input())
v = []
for i in range(n):
v1 = list(map(int, input().split()))
v.append(v1)
r = 0
for y in range(n - 1):
for x in range(n - y - 2, -1, -1):
if v[y][x] == v[n - y - 1][n - x - 2]:
r += 1
print(r)