 # Find "1" from (5X5) matrix made by "0" and print total minimal move 1 position to the center of this matrix in python

I’ll take a (5X5) matrix as input made by “0” with one “1”. Then print the minimal move from “1” position to the center of matrix.
It’s like if input:

``````0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
``````

Then output is:
`3`
Here is my code:

``````li = [list(map(int,input().split())) for x in range(5)]

for x in li:
kk = li.index(x)+1
if sum(x) == 1:
if kk == 3:
for j in x:
if j == 1:
k = (x.index(j)+1)
if k == 3:
print(0)
elif k < 3:
print(3-k)
elif k > 3:
print(k-3)
elif kk < 3:
for j in x:
if j == 1:
k = (x.index(j)+1)
if k == 3:
print(0+(3-kk))
elif k < 3:
print((3-k)+(3-kk))
elif k > 3:
print((k-3)+(3-kk))
elif kk > 3:
for j in x:
if j == 1:
k = (x.index(j)+1)
if k == 3:
print(0+(kk-3))
elif k < 3:
print((3-k)+(kk-3))
elif k > 3:
print((k-3)+(kk-3))

``````

I solved this.But why too long ,
How can i make my code more efficient?

Why not just find the i, j index of the 1 and do a little bit of arithmetic to compute the distance from 2,2? Then you’ll have something that will generalize to different size matrices nicely.

1 Like