You are viewing a single comment's thread. Return to all comments →
def countLuck(matrix, k): for i in range(n):#find start and end for j in range(m): if matrix[i][j]=='M': start=(i,j) if matrix[i][j]=='*': end=(i,j) vist={}#dic for if visited cur=[(start,0)]#record every step while cur: nex=[] for (p,q),turn in cur: if (p,q)==end: #if find end then stop return 'Impressed' if turn==k else 'Oops!' if (p,q) not in vist: #expansion steps vist[(p,q)]=turn nexpoint=[] for (i,j) in [(0,1),(0,-1),(-1,0),(1,0)]: if 0<=p+i<n and 0<=q+j<m and matrix[p+i][q+j]!='X' and (p+i,q+j) not in vist: nexpoint.append((p+i,q+j)) #if expansion steps >= 2 then turnning point+1 count =1 if len(nexpoint)>=2 else 0 #push new step into next step for (i,j) in nexpoint: nex.append(((i,j),turn+count)) cur=nex #next become current step
Seems like cookies are disabled on this browser, please enable them to open this website
Count Luck
You are viewing a single comment's thread. Return to all comments →
Python 3: