Find all the queens attacking the king in a chessboard | Java program to implement the above approach ; Method to find the queen closest to king in an attacking position ; Method to find all the queens attacking the king in the chessboard ; 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
import java . io . * ; import java . util . * ; import java . util . stream . Collectors ; class GFG { private static int dis ( int [ ] ans , int [ ] attacker ) { return Math . abs ( ans [ 0 ] - attacker [ 0 ] ) + Math . abs ( ans [ 1 ] - attacker [ 1 ] ) ; } private static List < List < Integer > > findQueens ( int [ ] [ ] queens , int [ ] king ) { List < List < Integer > > sol = new ArrayList < List < Integer > > ( ) ; int [ ] [ ] attackers = new int [ 8 ] [ 2 ] ; for ( int i = 0 ; i < 8 ; i ++ ) { Arrays . fill ( attackers [ i ] , - 1 ) ; } for ( int i = 0 ; i < queens . length ; i ++ ) { if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] > queens [ i ] [ 1 ] ) { if ( ( attackers [ 3 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 3 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 3 ] = queens [ i ] ; } if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] < queens [ i ] [ 1 ] ) { if ( ( attackers [ 4 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 4 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 4 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 0 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 0 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 0 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 7 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 7 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 7 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 1 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 1 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 1 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 6 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 6 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 6 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 2 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 2 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 2 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 5 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 5 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 5 ] = queens [ i ] ; } } for ( int i = 0 ; i < 8 ; i ++ ) if ( attackers [ i ] [ 0 ] != - 1 ) sol . add ( Arrays . stream ( attackers [ i ] ) . boxed ( ) . collect ( Collectors . toList ( ) ) ) ; return sol ; } private static void print ( List < List < Integer > > ans ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) System . out . print ( ans . get ( i ) . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] king = { 2 , 3 } ; int [ ] [ ] queens = { { 0 , 1 } , { 1 , 0 } , { 4 , 0 } , { 0 , 4 } , { 3 , 3 } , { 2 , 4 } } ; List < List < Integer > > ans = findQueens ( queens , king ) ; print ( ans ) ; } }