Deloitte | COGNIZANT | IBM | CAPGEMINI
from collections import deque
def minMovesToHospital(cls, input1, input2, input3):
N = input1
E = input2
K = input3 - 1
if E[K] == 0:
return 0
visited = [False] * N
q = deque([(K, 0)])
visited[K] = True
while q:
idx, moves = q.popleft()
for nxt in [idx + E[idx], idx - E[idx]]:
if 0 <= nxt < N and not visited[nxt]:
if E[nxt] == 0:
return moves + 1
visited[nxt] = True
q.append((nxt, moves + 1))
return -1
//MINIMUM MOVES✅
VIRTUSA✅
661 ·