📌  相关文章
📜  国王仅需M步即可在棋盘上到达的总位置|套装2

📅  最后修改于: 2021-04-22 06:01:30             🧑  作者: Mango

给定国王在8 X 8棋盘上的位置,任务是计算m步中国王可以访问的正方形总数。国王的位置用行号和列号表示。
注意:国王当前获取的正方形已经访问过,并将计入结果中。

例子:

方法:国王可以沿任何方向(即,水平,垂直和对角线)移动一个正方形。因此,国王可以一口气访问其相邻的广场。
棋王
因此,可以在m个动作中访问距国王当前位置m个单位距离以内的正方形(将1个正方形视为1个单位距离)。

  1. 对于棋盘上的所有正方形,检查特定正方形是否距King当前位置m单位距离或更小。
  2. 如果第1步为true,则增加计数。
  3. 打印计数

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of squares
// that can be visited by king in m moves
int countSquares(int r, int c, int m)
{
  
    // To store the count of squares
    int squares = 0;
  
    // Check all squares of
    // the chessboard
    for (int i = 1; i <= 8; i++) {
        for (int j = 1; j <= 8; j++) {
  
            // Check if square (i, j) is
            // at a distance <= m units
            // from king's current position
            if (max(abs(i - r), abs(j - c)) <= m)
                squares++;
        }
    }
  
    // Return count of squares
    return squares;
}
  
// Driver code
int main()
{
    int r = 4, c = 4, m = 1;
  
    cout << countSquares(r, c, m) << endl;
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the count of squares
    // that can be visited by king in m moves
    static int countSquares(int r, int c, int m)
    {
        // To store the count of squares
        int squares = 0;
  
        // Check all squares of
        // the chessboard
        for (int i = 1; i <= 8; i++) {
            for (int j = 1; j <= 8; j++) {
  
                // Check if square (i, j) is
                // at a distance <= m units
                // from king's current position
                if (Math.max(Math.abs(i - r), Math.abs(j - c)) <= m)
                    squares++;
            }
        }
  
        // Return count of squares
        return squares;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int r = 4, c = 4, m = 1;
        System.out.print(countSquares(r, c, m));
    }
}


C#
// C# implementation of the approach
using System;
class GFG {
  
    // Function to return the count of squares
    // that can be visited by king in m moves
    static int countSquares(int r, int c, int m)
    {
        // To store the count of squares
        int squares = 0;
  
        // Check all squares of
        // the chessboard
        for (int i = 1; i <= 8; i++) {
            for (int j = 1; j <= 8; j++) {
  
                // Check if square (i, j) is
                // at a distance <= m units
                // from king's current position
                if (Math.Max(Math.Abs(i - r), Math.Abs(j - c)) <= m)
                    squares++;
            }
        }
  
        // Return count of squares
        return squares;
    }
  
    // Driver code
    public static void Main()
    {
        int r = 4, c = 4, m = 1;
        Console.Write(countSquares(r, c, m));
    }
}


Python3
# Python implementation of the approach
  
# Function to return the count of squares
# that can be visited by king in m moves
def countSquares(r, c, m):
  
    # To store the count of squares
    squares = 0
      
    # Check all squares of 
    # the chessboard
    for i in range (1, 9):
        for j in range (1, 9):
              
            # Check if square (i, j) is
            # at a distance <= m units
            # from king's current position
            if(max(abs(i - r), abs(j - c)) <= m):
                squares = squares + 1
          
    # Return count of squares
    return squares
  
# Driver code
r = 4
c = 4
m = 1
  
print(countSquares(r, c, m));


PHP


输出:
9