给定Bishop和Pawn在8 * 8棋盘上的位置,任务是检查Bishop是否可以一举将Pawn放下。 Bishop和Pawn的位置用棋盘的行号和列号表示。
例子:
Input: bishopX = 5, bishopY = 5, pawnX = 1, pawnY = 1
Output: Yes
Input: bishopX = 5, bishopY = 5, pawnX = 1, pawnY = 3
Output: No
方法:在象棋游戏中,主教只能沿对角线移动,并且每次移动的距离都没有限制。如果我们以Bishop的位置为原点,可以得出结论,Bishop只能以45度, 135度, 225度和315度角进行攻击。因此,主教只有在处于从其位置获得的倾斜等于45度或135度或225度或315度角的切线的位置时,才可以攻击Pawn。
我们知道两点的斜率方程
还,
对于Bishop的所有迎角(即45°,135°,225°和315°),坡度值将为1或-1。
tan(45°) = 1
tan(135°) = -1
tan(225°) = 1
tan(315°) = -1
因此,对于45°和225°角,斜率方程为
x2 – x1 = y2 – y1
对于135°和315°角,斜率方程为
-x2 + x1 = y2 – y1
- 以Bishop和Pawn的位置为二维坐标上的点,请检查两个方程式是否为真
- x2 – x1 = y2 – y1
- -x2 + x1 = y2 – y1
- 否则打印No。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function that return true if
// the Bishop can take down the pawn
bool canTakeDown(int bishopX, int bishopY, int pawnX, int pawnY)
{
// If pawn is at angle
// 45 or 225 degree from
// bishop's Position
if (pawnX - bishopX == pawnY - bishopY)
return true;
// If pawn is at angle
// 135 or 315 degree from
// bishop's Position
else if (-pawnX + bishopX == pawnY - bishopY)
return true;
else
return false;
}
// Driver code
int main()
{
// Bishop's Position
int bishopX = 5, bishopY = 5;
// Pawn's Position
int pawnX = 1, pawnY = 1;
if (canTakeDown(bishopX, bishopY, pawnX, pawnY))
cout << "Yes";
else
cout << "No";
}
Java
// Java implementation of above approach
class GFG {
// Function that return true if
// the Bishop can take down the pawn
static boolean canTakeDown(int bishopX, int bishopY, int pawnX, int pawnY)
{
// If pawn is at angle
// 45 or 225 degree from
// bishop's Position
if (pawnX - bishopX == pawnY - bishopY)
return true;
// If pawn is at angle
// 135 or 315 degree from
// bishop's Position
else if (-pawnX + bishopX == pawnY - bishopY)
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
// Bishop's Position
int bishopX = 5, bishopY = 5;
// Pawn's Position
int pawnX = 1, pawnY = 1;
if (canTakeDown(bishopX, bishopY, pawnX, pawnY))
System.out.println("Yes");
else
System.out.println("No");
}
}
Python3
# Python3 implementation of above approach
# Function that return true if
# the Bishop can take down the pawn
def canTakeDown(bishopX, bishopY, pawnX, pawnY) :
# If pawn is at angle
# 45 or 225 degree from
# bishop's Position
if (pawnX - bishopX == pawnY - bishopY) :
return True
# If pawn is at angle
# 135 or 315 degree from
# bishop's Position
elif (-pawnX + bishopX == pawnY - bishopY):
return True
else:
return False
# Driver code
# Bishop's Position
bishopX = 5
bishopY = 5
# Pawn's Position
pawnX = 1
pawnY = 1
if (canTakeDown(bishopX, bishopY, pawnX, pawnY)) :
print("Yes")
else :
print("No")
C#
// C# implementation of above approach
using System;
class GFG {
// Function that return true if
// the Bishop can take down the pawn
static bool canTakeDown(int bishopX, int bishopY, int pawnX, int pawnY)
{
// If pawn is at angle
// 45 or 225 degree from
// bishop's Position
if (pawnX - bishopX == pawnY - bishopY)
return true;
// If pawn is at angle
// 135 or 315 degree from
// bishop's Position
else if (-pawnX + bishopX == pawnY - bishopY)
return true;
else
return false;
}
// Driver code
public static void Main()
{
// Bishop's Position
int bishopX = 5, bishopY = 5;
// Pawn's Position
int pawnX = 1, pawnY = 1;
if (canTakeDown(bishopX, bishopY, pawnX, pawnY))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
PHP
输出:
Yes