We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Really liked the problem, basically used everything there are with bfs.
Python solution:
defis_valid(i,j,n):ifi>=0andi<nandj>=0andj<n:returnTruereturnFalsedefget_neighbours(loc,n):i,j=loc[0],loc[1]name=['UL','UR','R','LR','LL','L']dr=[-2,-2,0,2,2,0]cr=[-1,1,2,1,-1,-2]res=[]fordinrange(6):ifis_valid(i+dr[d],j+cr[d],n):res.append([(i+dr[d],j+cr[d]),name[d]])returnresdefprintShortestPath(n,i_start,j_start,i_end,j_end):# Print the distance along with the sequence of moves.parent_dict={(i_start,j_start):None}front_tier=[(i_start,j_start)]_next=[]lvl=0tgt=(i_end,j_end)defprint_res(v):res=[]cur=vwhileTrue:parent=parent_dict[cur]ifparentisNone:foriinrange(len(res)-1,-1,-1):print(res[i],end=' ')returnres.append(parent[1])cur=parent[0]whilefront_tier:forvinfront_tier:ifv==tgt:print(lvl)print_res(v)returnneighbours=get_neighbours(v,n)forneighbourinneighbours:ifneighbour[0]notinparent_dict:_next.append(neighbour[0])parent_dict[neighbour[0]]=[v,neighbour[1]]lvl+=1front_tier=_next_next=[]print('Impossible')
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Red Knight's Shortest Path
You are viewing a single comment's thread. Return to all comments →
Really liked the problem, basically used everything there are with bfs.
Python solution: