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++
#include <iostream>
using namespace std;
bool canQueenAttack( int qR, int qC, int oR, int oC)
{
if (qR == oR)
return true ;
if (qC == oC)
return true ;
if ( abs (qR - oR) == abs (qC - oC))
return true ;
return false ;
}
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;
}
|
Java
class GFG
{
static boolean canQueenAttack( int qR, int qC,
int oR, int oC)
{
if (qR == oR)
return true ;
if (qC == oC)
return true ;
if (Math.abs(qR - oR) == Math.abs(qC - oC))
return true ;
return false ;
}
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" );
}
}
|
Python3
def canQueenAttack(qR, qC, oR, oC):
if qR = = oR:
return True
if qC = = oC:
return True
if abs (qR - oR) = = abs (qC - oC):
return True
return False
if __name__ = = "__main__" :
qR, qC = 1 , 1
oR, oC = 3 , 2
if canQueenAttack(qR, qC, oR, oC):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool canQueenAttack( int qR, int qC,
int oR, int oC)
{
if (qR == oR)
return true ;
if (qC == oC)
return true ;
if (Math.Abs(qR - oR) == Math.Abs(qC - oC))
return true ;
return false ;
}
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" );
}
}
|
PHP
<?php
function canQueenAttack( $qR , $qC , $oR , $oC )
{
if ( $qR == $oR )
return true;
if ( $qC == $oC )
return true;
if ( abs ( $qR - $oR ) == abs ( $qC - $oC ))
return true;
return false;
}
$qR = 1; $qC = 1;
$oR = 3; $oC = 2;
if (canQueenAttack( $qR , $qC , $oR , $oC ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function canQueenAttack(qR, qC, oR, oC)
{
if (qR == oR)
return true ;
if (qC == oC)
return true ;
if (Math.abs(qR - oR) == Math.abs(qC - oC))
return true ;
return false ;
}
var qR = 1, qC = 1;
var oR = 3, oC = 2;
if (canQueenAttack(qR, qC, oR, oC))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Sep, 2022
Like Article
Save Article