Find all the queens attacking the king in a chessboard | Function to find the queen closest to king in an attacking position ; Function to find all the queens attacking the king in the chessboard ; Iterating over the coordinates of the queens ; If king is horizontally on the right of current queen ; If no attacker is present in that direction ; Or if the current queen is closest in that direction ; Set current queen as the attacker ; If king is horizontally on the left of current queen ; If no attacker is present in that direction ; Or if the current queen is closest in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the left by a queen diagonal above ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the left by a queen diagonally below ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the right by a queen diagonally above ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the right by a queen diagonally below ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If a king is vertically below the current queen ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If a king is vertically above the current queen ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; Return the coordinates ; Print all the coordinates of the queens attacking the king ; Driver Code
def dis ( ans , attacker ) : NEW_LINE INDENT return ( abs ( ans [ 0 ] - attacker [ 0 ] ) + abs ( ans [ 1 ] - attacker [ 1 ] ) ) NEW_LINE DEDENT def findQueens ( queens , king ) : NEW_LINE INDENT sol = [ ] NEW_LINE attackers = [ [ 0 for x in range ( 8 ) ] for y in range ( 8 ) ] NEW_LINE for i in range ( len ( queens ) ) : NEW_LINE INDENT if ( king [ 0 ] == queens [ i ] [ 0 ] and king [ 1 ] > queens [ i ] [ 1 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 3 ] ) == 0 ) or ( ( dis ( attackers [ 3 ] , king ) > dis ( queens [ i ] , king ) ) ) ) : NEW_LINE INDENT attackers [ 3 ] = queens [ i ] ; NEW_LINE DEDENT DEDENT if ( king [ 0 ] == queens [ i ] [ 0 ] and king [ 1 ] < queens [ i ] [ 1 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 4 ] ) == 0 ) or ( dis ( attackers [ 4 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 4 ] = queens [ i ] ; NEW_LINE DEDENT DEDENT if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] and king [ 0 ] > queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 0 ] ) == 0 ) or ( dis ( attackers [ 0 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 0 ] = queens [ i ] NEW_LINE DEDENT DEDENT if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] and king [ 0 ] < queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 7 ] ) == 0 ) or ( dis ( attackers [ 7 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 7 ] = queens [ i ] NEW_LINE DEDENT DEDENT if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 and king [ 0 ] > queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 1 ] ) == 0 ) or ( dis ( attackers [ 1 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 1 ] = queens [ i ] NEW_LINE DEDENT DEDENT if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 and king [ 0 ] < queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 6 ] ) == 0 ) or ( dis ( attackers [ 6 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 6 ] = queens [ i ] ; NEW_LINE DEDENT DEDENT if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) and king [ 0 ] > queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 2 ] ) == 0 ) or ( dis ( attackers [ 2 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 2 ] = queens [ i ] NEW_LINE DEDENT DEDENT if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) and king [ 0 ] < queens [ i ] [ 0 ] ) : NEW_LINE INDENT if ( ( len ( attackers [ 5 ] ) == 0 ) or ( dis ( attackers [ 5 ] , king ) > dis ( queens [ i ] , king ) ) ) : NEW_LINE INDENT attackers [ 5 ] = queens [ i ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( 8 ) : NEW_LINE INDENT f = 1 NEW_LINE for x in attackers [ i ] : NEW_LINE if x != 0 : NEW_LINE INDENT f = 0 NEW_LINE break NEW_LINE DEDENT if f == 0 : NEW_LINE sol . append ( attackers [ i ] ) NEW_LINE DEDENT return sol NEW_LINE DEDENT def print_board ( ans ) : NEW_LINE INDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT print ( ans [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT king = [ 2 , 3 ] NEW_LINE queens = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 4 , 0 ] , [ 0 , 4 ] , [ 3 , 3 ] , [ 2 , 4 ] ] NEW_LINE ans = findQueens ( queens , king ) ; NEW_LINE print_board ( ans ) ; NEW_LINE DEDENT