Find all the queens attacking the king in a chessboard | C ++ Program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dis ( vector < int > ans , vector < int > attacker ) { return abs ( ans [ 0 ] - attacker [ 0 ] ) + abs ( ans [ 1 ] - attacker [ 1 ] ) ; } vector < vector < int > > findQueens ( vector < vector < int > > & queens , vector < int > & king ) { vector < vector < int > > sol ; vector < vector < int > > attackers ( 8 ) ; for ( int i = 0 ; i < queens . size ( ) ; i ++ ) { if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] > queens [ i ] [ 1 ] ) { if ( ( attackers [ 3 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( 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 ] . size ( ) == 0 ) || ( dis ( attackers [ 5 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 5 ] = queens [ i ] ; } } for ( int i = 0 ; i < 8 ; i ++ ) if ( attackers [ i ] . size ( ) ) sol . push_back ( attackers [ i ] ) ; return sol ; } void print ( vector < vector < int > > ans ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) cout << ans [ i ] [ j ] << " ▁ " ; cout << " STRNEWLINE " ; } } int main ( ) { vector < int > king = { 2 , 3 } ; vector < vector < int > > queens = { { 0 , 1 } , { 1 , 0 } , { 4 , 0 } , { 0 , 4 } , { 3 , 3 } , { 2 , 4 } } ; vector < vector < int > > ans = findQueens ( queens , king ) ; print ( ans ) ; }