A matrix in Python is just a list of lists: mat[r] is row r and mat[r][c] is the cell in row r, column c. For a square matrix with n rows, the primary diagonal runs from the top-left to the bottom-right, and the secondary diagonal runs from the top-right to the bottom-left.
In a board game on a square grid mat, every cell is worth some points (a few cells cost points). A player who draws a big X from corner to corner scores every cell the X passes through: all of the primary diagonal and all of the secondary diagonal. On an odd-sized board the two strokes cross in the centre cell, and that cell scores only once. Return the X's score.
Examples
Input: mat = [[2, 0, 1],
[3, 4, 5],
[6, 1, 7]]
Output: 20
Explanation: 2 + 4 + 7 (primary) + 1 + 6 (secondary). The centre 4 is counted once.
Input: mat = [[3, 1, 1, 2],
[1, 5, 0, 1],
[1, 2, 4, 1],
[4, 1, 1, 3]]
Output: 23
Explanation: on an even-sized board the strokes share no cell: 3 + 5 + 4 + 3 + 2 + 0 + 2 + 4.
Constraints
mathasnrows andncolumns,1 <= n <= 50-100 <= mat[r][c] <= 100
Goals
- Index a 2D list with mat[row][col]
- Derive the column of a diagonal cell from its row number
- Avoid double counting a cell that lies on both diagonals