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.
defmoveNight(a,b,n,loc):fordx,dyin[[a,b],[b,a]]:forsx,syin[[1,1],[-1,1],[1,-1],[-1,-1]]:x=loc[0]+dx*sxy=loc[1]+dy*syifany([x<0,y<0,x>n-1,y>n-1]):# Out of boundscontinueyieldx,ydefbreadthF(a,b,n):traveled=set(((0,0),))level=set(((0,0),))d=1whilelevel:newLevel=set()forlocinlevel:forx,yinmoveNight(a,b,n,loc):if(x,y)==(n-1,n-1):returndif(x,y)notintraveled:traveled.add((x,y))newLevel.add((x,y))level=newLeveld+=1return-1defknightlOnAChessboard(n):# Loop rows and columnsans=[[breadthF(row,col,n)forcolinrange(1,n)]forrowinrange(1,n)]returnans
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
KnightL on a Chessboard
You are viewing a single comment's thread. Return to all comments →
python3