Dada la posición de la reina (qX, qY) y el oponente (oX, oY) en un tablero de ajedrez. La tarea es determinar si la reina puede atacar al oponente o no. Tenga en cuenta que la dama puede atacar en la misma fila, la misma columna y en diagonal.
Ejemplo:
Entrada: qX = 4, qY = 5, oX = 6, oY = 7
Salida: Sí
La dama puede atacar en diagonal.
Entrada: qX = 1, qY = 1, oX = 3, oY = 2
Salida: No
Acercarse:
- Si qR = oR , significa que tanto la reina como el oponente están en la misma fila y la reina puede atacar al oponente.
- De manera similar, si qC = oC , entonces también la dama puede atacar al oponente ya que ambos están en la misma columna.
- Y para las diagonales, si abs(qR – oR) = abs(qC – oC) es decir, la reina puede atacar al oponente en diagonal.
Si todas las condiciones anteriores fallan, el oponente está a salvo de la dama.
A continuación se muestra la implementación del enfoque anterior:
C++
// 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; }
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
<?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 ?>
Javascript
<script> // JavaScript 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 (Math.abs(qR - oR) == Math.abs(qC - oC)) return true; // Opponent is safe return false; } // Driver code var qR = 1, qC = 1; var oR = 3, oC = 2; if (canQueenAttack(qR, qC, oR, oC)) document.write("Yes"); else document.write("No"); // This Code is Contributed by Harshit Srivastava </script>
Producción:
No