Check if a Queen can attack a given cell on chessboard

Given the position of the queen (qX, qY) and the opponent (oX, oY) on a chessboard. The task is to determine whether the queen can attack the opponent or not. Note that the queen can attack in the same row, same column and diagonally.

Example:

Input: qX = 4, qY = 5, oX = 6, oY = 7
Output: Yes
The queen can attack diagonally.

Input: qX = 1, qY = 1, oX = 3, oY = 2
Output: No

Approach:

  • If qR = oR, it means that both the queen and the opponent are in the same row and the queen can attack the opponent.
  • Similarly, if qC = oC then also the queen can attack the opponent as they both are in the same column.
  • And for diagonals, if abs(qR – oR) = abs(qC – oC) i.e. queen can attack the opponent diagonally.

If all of the above conditions fail then the opponent is safe from the queen.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function that returns true if the queen
// can attack the opponent
bool canQueenAttack(int qR, int qC, int oR, int oC)
{
    // If queen and the opponent are in the same row
    if (qR == oR)
        return true;
  
    // If queen and the opponent are in the same column
    if (qC == oC)
        return true;
  
    // If queen can attack diagonally
    if (abs(qR - oR) == abs(qC - oC))
        return true;
  
    // Opponent is safe
    return false;
}
  
// Driver code
int main()
{
    int qR = 1, qC = 1;
    int oR = 3, oC = 2;
    if (canQueenAttack(qR, qC, oR, oC))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function that returns true if the queen
// can attack the opponent
static boolean canQueenAttack(int qR, int qC,
                                int oR, int oC)
{
    // If queen and the opponent
    // are in the same row
    if (qR == oR)
        return true;
  
    // If queen and the opponent 
    // are in the same column
    if (qC == oC)
        return true;
  
    // If queen can attack diagonally
    if (Math.abs(qR - oR) == Math.abs(qC - oC))
        return true;
  
    // Opponent is safe
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    int qR = 1, qC = 1;
    int oR = 3, oC = 2;
    if (canQueenAttack(qR, qC, oR, oC))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is Contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function that returns True if the 
# queen can attack the opponent 
def canQueenAttack(qR, qC, oR, oC): 
  
    # If queen and the opponent are
    # in the same row 
    if qR == oR:
        return True
  
    # If queen and the opponent are 
    # in the same column 
    if qC == oC:
        return True
  
    # If queen can attack diagonally 
    if abs(qR - oR) == abs(qC - oC): 
        return True
  
    # Opponent is safe 
    return False
  
# Driver code 
if __name__ == "__main__"
  
    qR, qC = 1, 1
    oR, oC = 3, 2
    if canQueenAttack(qR, qC, oR, oC): 
        print("Yes")
    else:
        print("No"
      
# This code is contributed 
# by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG
{
      
// Function that returns true if the queen
// can attack the opponent
static bool canQueenAttack(int qR, int qC,
                                int oR, int oC)
{
    // If queen and the opponent
    // are in the same row
    if (qR == oR)
        return true;
  
    // If queen and the opponent 
    // are in the same column
    if (qC == oC)
        return true;
  
    // If queen can attack diagonally
    if (Math.Abs(qR - oR) == Math.Abs(qC - oC))
        return true;
  
    // Opponent is safe
    return false;
}
  
// Driver code
public static void Main()
{
    int qR = 1, qC = 1;
    int oR = 3, oC = 2;
    if (canQueenAttack(qR, qC, oR, oC))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is Contributed by Code_Mech.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that returns true if the 
// queen can attack the opponent
function canQueenAttack($qR, $qC, $oR, $oC)
{
    // If queen and the opponent are 
    // in the same row
    if ($qR == $oR)
        return true;
  
    // If queen and the opponent are
    // in the same column
    if ($qC == $oC)
        return true;
  
    // If queen can attack diagonally
    if (abs($qR - $oR) == abs($qC - $oC))
        return true;
  
    // Opponent is safe
    return false;
}
  
// Driver code
$qR = 1; $qC = 1;
$oR = 3; $oC = 2;
  
if (canQueenAttack($qR, $qC, $oR, $oC))
    echo "Yes";
else
    echo "No";
  
// This code is contributed
// by Akanksha Rai
?>

chevron_right


Output:

No


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.