You are given an M*N matrix and K, write a program to compute the area of the sub-matrix and print the result.
A sub-matrix is a matrix formed after deleting K rows each from the top and bottom and K columns each from left and right.
Area of a matrix is defined as the product of all elements of the matrix.
Explanation
For M = 5 and N = 4, the matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
For K = 1, the sub-matrix is as follows:
6 7
10 11
14 15
Therefore, the area of the given matrix is 6*7*10*11*14*15 = 970200.
Sample Input1
5 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
1
Sample Output1
970200
def readMatrix():
line = input()
m = int(line.split()[0])
n = int(line.split()[1])
M = []
for i in range(m):
line = input()
row = [int(s) for s in line.split()]
M.append(row)
return M
def subMatrix(M, k):
m = len(M)
n = len(M[0])
if m <= 2*k or n <= 2*k:
return [[]]
S = []
for i in range(1, m-1):
S.append(M[i][k:-k])
return S
def areaMatrix(M):
a = 1
for row in M:
for x in row:
a *= x
return a
def main():
M = readMatrix()
k = int(input())
M = subMatrix(M, k)
s = areaMatrix(M)
print(s)
if __name__ == '__main__':
main()
Comments
Leave a comment