📌  相关文章
📜  检查女王是否可以攻击棋盘上的给定单元

📅  最后修改于: 2021-06-27 02:43:45             🧑  作者: Mango

给定皇后(qX,qY)和对手(oX,oY)在棋盘上的位置。任务是确定女王是否可以攻击对手。注意,女王可以在同一行,同一列和对角线进攻。
例子:

方法:

  • 如果qR = oR ,则意味着皇后和对手都在同一行中,皇后可以攻击对手。
  • 同样,如果qC = oC,则女王也可以攻击对手,因为他们都在同一列中。
  • 对于对角线,如果abs(qR – oR)= abs(qC – oC),即女王可以对角线攻击对手。

如果以上所有条件均不成立,则对手可以避开女王。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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.


Python3
# 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


C#
// 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.


PHP


Javascript


输出:
No

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。