📜  计算主教一口气可以访问的广场总数

📅  最后修改于: 2021-06-25 21:43:10             🧑  作者: Mango

给定主教8 * 8棋盘上的位置,任务是计算主教一举可以访问的正方形总数。主教的位置用棋盘的行号和列号表示。
例子:

方法:在国际象棋游戏中,主教只能对角移动,并且每次移动的距离都没有限制。

因此,我们也可以说Bishop可以以四种方式移动,即从当前位置对角线左上,右上,左下和右下。
我们可以通过以下方式计算每次移动中访问的平方数:

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to return the count of
// total positions the Bishop
// can visit in a single move
int countSquares(int row, int column)
{
 
    // Count top left squares
    int topLeft = min(row, column) - 1;
 
    // Count bottom right squares
    int bottomRight = 8 - max(row, column);
 
    // Count top right squares
    int topRight = min(row, 9 - column) - 1;
 
    // Count bottom left squares
    int bottomLeft = 8 - max(row, 9 - column);
 
    // Return total count
    return (topLeft + topRight + bottomRight + bottomLeft);
}
 
// Driver code
int main()
{
 
    // Bishop's Position
    int row = 4, column = 4;
 
    cout << countSquares(row, column);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG {
 
    // Function to return the count of
    // total positions the Bishop
    // can visit in a single move
    static int countSquares(int row, int column)
    {
 
        // Count top left squares
        int topLeft = Math.min(row, column) - 1;
 
        // Count bottom right squares
        int bottomRight = 8 - Math.max(row, column);
 
        // Count top right squares
        int topRight = Math.min(row, 9 - column) - 1;
 
        // Count bottom left squares
        int bottomLeft = 8 - Math.max(row, 9 - column);
 
        // Return total count
        return (topLeft + topRight + bottomRight + bottomLeft);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Bishop's Position
        int row = 4, column = 4;
 
        System.out.println(countSquares(row, column));
    }
}


C#
// C# implementation of above approach
using System;
class GFG {
 
    // Function to return the count of
    // total positions the Bishop
    // can visit in a single move
    static int countSquares(int row, int column)
    {
 
        // Count top left squares
        int topLeft = Math.Min(row, column) - 1;
 
        // Count bottom right squares
        int bottomRight = 8 - Math.Max(row, column);
 
        // Count top right squares
        int topRight = Math.Min(row, 9 - column) - 1;
 
        // Count bottom left squares
        int bottomLeft = 8 - Math.Max(row, 9 - column);
 
        // Return total count
        return (topLeft + topRight + bottomRight + bottomLeft);
    }
 
    // Driver code
    public static void Main()
    {
 
        // Bishop's Position
        int row = 4, column = 4;
 
        Console.WriteLine(countSquares(row, column));
    }
}


Python3
# Python3 implementation of above approach
 
# Function to return the count of
# total positions the Bishop
# can visit in a single move
def countSquares(row, column):
     
    # Count top left squares
    topLeft = min(row, column) - 1
     
    # Count bottom right squares
    bottomRight = 8 - max(row, column)
     
    # Count top right squares
    topRight = min(row, 9-column) -1
     
    # Count bottom left squares
    bottomLeft = 8 - max(row, 9-column)
 
     
    # Return total count
    return (topLeft + topRight + bottomRight + bottomLeft)
 
# Driver code
 
# Bishop's Position
row = 4
column = 4
 
print(countSquares(row, column))


PHP


Javascript


输出:
13

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