Queen's Attack II Discussions | Algorithms | HackerRank
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.
Python 3
My code runs by doing a few things for each calculation:
1. determine all the obstacles that are in line with my queen (above and below, left and right, top left to bottom right diagonal, and top right to bottom left diagonal)
2. to count an attackable square it needs to be: in bounds and before the first object in line with the queen in the given direction
3. for the 4 cardinal directions (up, down, left, right) I only need to check the appriate row or column, for the 4 ordinal directions (the diagonals) I must check for the appropriate row and column
4. I finally add the available attacks from all 8 directions and return it
defqueensAttack(n,k,r_q,c_q,obstacles):# Write your code hereattacks=0#upsame_col=[obsforobsinobstaclesifobs[1]==c_q]above_r=r_q+1up_attacks=0whileabove_r<=nandall(above_r<obs[0]forobsinsame_colifobs[0]>r_q):up_attacks+=1above_r+=1#print(f'above: {up_attacks}')#debugging#downbelow_r=r_q-1down_attacks=0whilebelow_r>0andall(below_r>obs[0]forobsinsame_colifobs[0]<r_q):down_attacks+=1below_r-=1#print(f'below: {down_attacks}')#debugging#leftsame_row=[obsforobsinobstaclesifobs[0]==r_q]left_c=c_q-1left_attacks=0whileleft_c>0andall(left_c>obs[1]forobsinsame_rowifobs[1]<c_q):left_attacks+=1left_c-=1#print(f'left: {left_attacks}')#debugging#rightright_c=c_q+1right_attacks=0whileright_c<=nandall(right_c<obs[1]forobsinsame_rowifobs[1]>c_q):right_attacks+=1right_c+=1#print(f'right: {right_attacks}')#debugging#diag up leftleft_right_diag=[obsforobsinobstaclesifobs[0]-r_q==c_q-obs[1]]up_left_row=r_q+1up_left_col=c_q-1up_left_attacks=0whileup_left_row<=nandup_left_col>0andall(up_left_row<obs[0]andup_left_col>obs[1]forobsinleft_right_diagifobs[0]>r_qandobs[1]<c_q):up_left_attacks+=1up_left_row+=1up_left_col-=1#print(f'up left: {up_left_attacks}')#debugging#diag down rightdown_right_row=r_q-1down_right_col=c_q+1down_right_attacks=0whiledown_right_row>0anddown_right_col<=nandall(down_right_row>obs[0]anddown_right_col<obs[1]forobsinleft_right_diagifobs[0]<r_qandobs[1]>c_q):down_right_attacks+=1down_right_row-=1down_right_col+=1#print(f'down right: {down_right_attacks}') #debugging#diag up rightright_left_diag=[obsforobsinobstaclesifobs[0]-r_q==obs[1]-c_q]up_right_row=r_q+1up_right_col=c_q+1up_right_attacks=0whileup_right_row<=nandup_right_col<=nandall(up_right_row<obs[0]andup_right_col<obs[1]forobsinright_left_diagifobs[0]>r_qandobs[1]>c_q):up_right_attacks+=1up_right_row+=1up_right_col+=1#print(f'up right: {up_right_attacks}') #debugging#diag down leftdown_left_row=r_q-1down_left_col=c_q-1down_left_attacks=0whiledown_left_row>0anddown_left_col>0andall(down_left_row>obs[0]anddown_left_col>obs[1]forobsinright_left_diagifobs[0]<r_qandobs[1]<c_q):down_left_attacks+=1down_left_row-=1down_left_col-=1#print(f'down left: {down_left_attacks}') #debuggingattacks=up_attacks+down_attacks+left_attacks+right_attacks+up_left_attacks+down_right_attacks+up_right_attacks+down_left_attacks#print(attacks) #debuggingreturnattacks
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
Python 3 My code runs by doing a few things for each calculation: 1. determine all the obstacles that are in line with my queen (above and below, left and right, top left to bottom right diagonal, and top right to bottom left diagonal) 2. to count an attackable square it needs to be: in bounds and before the first object in line with the queen in the given direction 3. for the 4 cardinal directions (up, down, left, right) I only need to check the appriate row or column, for the 4 ordinal directions (the diagonals) I must check for the appropriate row and column 4. I finally add the available attacks from all 8 directions and return it