#P11001. Diagonal Pair Counting
Diagonal Pair Counting
Diagonal Pair Counting
Given an $n \times m$ grid where each cell contains an integer $A_{i,j}$, count the number of pairs of cells \((a,b)\) and \((c,d)\) such that $A_{a,b} = A_{c,d}$ and $|a-c| = |b-d| > 0$.
A pair of cells is considered valid if they share the same integer value and they are located diagonally relative to each other, i.e. either along a main diagonal (top‐left to bottom‐right) or an anti-diagonal (top‐right to bottom‐left).
Hint: Notice that two cells \((a,b)\) and \((c,d)\) satisfy $|a-c| = |b-d|$ if and only if they lie on the same diagonal line. In fact, they belong to the same main diagonal if $a-b = c-d$, or the same anti-diagonal if $a+b=c+d$.
inputFormat
The first line contains two integers $n$ and $m$ representing the number of rows and columns respectively. Each of the following $n$ lines contains $m$ integers which represent the grid's cells.
outputFormat
Output a single integer denoting the total number of valid pairs satisfying the given condition.
sample
2 2
1 2
3 1
1