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.
I implemented a solution using a more intuitive and naive way for me where i explicitly coded both player logic which player1 always choose the wining position and player2 always choose the losing position. It's kinda mind blowing to me you can completly hide player2's logic by defining it to be player1 always force the next state to lose state after reading editorial. Just going to share my code here:
'''1=win,0=losep0=player1chooseanywinstatep1=player2chooseanylosestate'''defis_inbound(x,y):n=15ifx>nory>norx<1ory<1:returnFalsereturnTruehm={}defhelper(x,y,p):nxt_p=notpdelta=[(-2,1),(-2,-1),(1,-2),(-1,-2)]states=[]nxt_pos=[]res=0# check next possible movesfordindelta:xd,yd=x+d[0],y+d[1]ifis_inbound(xd,yd):nxt_pos.append((xd,yd))# no possible moveifnotnxt_pos:ifp:return1else:return0#recurseeveryvalidmovewithmemoforposinnxt_pos:cur=(pos[0],pos[1],nxt_p)ifcurinhm:state=hm[cur]else:state=helper(*cur)hm[cur]=statestates.append(state)# logic of optimal playres=all(states)ifpelseany(states)returnresdefchessboardGame(x,y):# Write your code hereres=helper(x,y,0)return'First'ifreselse'Second'
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Chessboard Game
You are viewing a single comment's thread. Return to all comments →
I implemented a solution using a more intuitive and naive way for me where i explicitly coded both player logic which player1 always choose the wining position and player2 always choose the losing position. It's kinda mind blowing to me you can completly hide player2's logic by defining it to be player1 always force the next state to lose state after reading editorial. Just going to share my code here: