📌  相关文章
📜  国王只需M步即可在棋盘上到达的总位置

📅  最后修改于: 2021-06-26 15:34:25             🧑  作者: Mango

给定一个整数M ,一个8 * 8的棋盘,国王放置在棋盘的正方形之一上。设国王的坐标为(R,C)
注意,只有在满足以下条件时,国王才能移动到坐标为(R1,C1)的正方形。

任务是计算在精确的M步中国王从给定正方形可以到达的位置数(不包括初始位置)。
例子:

方法:计算国王可以访问的左上角正方形的坐标(a,b)和国王可以访问的棋盘的右下角正方形(c,d)的坐标。那么国王可以访问的牢房总数将为(c – a + 1)*(d – b + 1)– 1
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to return the number of squares that
// the king can reach in the given number of moves
int Square(int row, int column, int moves)
{
    int a = 0, b = 0, c = 0, d = 0, total = 0;
 
    // Calculate initial and final coordinates
    a = row - moves;
    b = row + moves;
    c = column - moves;
    d = column + moves;
 
    // Since chessboard is of size 8X8 so if
    // any coordinate is less than 1 or greater than 8
    // make it 1 or 8.
    if (a < 1)
        a = 1;
    if (c < 1)
        c = 1;
    if (b > 8)
        b = 8;
    if (d > 8)
        d = 8;
 
    // Calculate total positions
    total = (b - a + 1) * (d - c + 1) - 1;
    return total;
}
 
// Driver code
int main()
{
    int R = 4, C = 5, M = 2;
    cout << Square(R, C, M);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
 
// Function to return the number
// of squares that the king can
// reach in the given number of moves
static int Square(int row, int column,
                            int moves)
{
    int a = 0, b = 0, c = 0,
        d = 0, total = 0;
 
    // Calculate initial and final coordinates
    a = row - moves;
    b = row + moves;
    c = column - moves;
    d = column + moves;
 
    // Since chessboard is of size 8X8
    // so if any coordinate is less
    // than 1 or greater than 8 make
    // it 1 or 8.
    if (a < 1)
        a = 1;
    if (c < 1)
        c = 1;
    if (b > 8)
        b = 8;
    if (d > 8)
        d = 8;
 
    // Calculate total positions
    total = (b - a + 1) * (d - c + 1) - 1;
    return total;
}
 
// Driver code
public static void main(String []args)
{
    int R = 4, C = 5, M = 2;
    System.out.println(Square(R, C, M));
}
}
 
// This code is contributed by Ita_c.


Python3
# Python3 implementation of above approach
 
# Function to return the number of
# squares that the king can reach
# in the given number of moves
def Square(row, column, moves) :
     
    a = 0; b = 0; c = 0;
    d = 0; total = 0;
 
    # Calculate initial and final
    # coordinates
    a = row - moves;
    b = row + moves;
    c = column - moves;
    d = column + moves;
 
    # Since chessboard is of size 8X8
    # so if any coordinate is less than
    # 1 or greater than 8 make it 1 or 8.
    if (a < 1) :
        a = 1;
    if (c < 1) :
        c = 1;
    if (b > 8) :
        b = 8;
    if (d > 8) :
        d = 8;
 
    # Calculate total positions
    total = (b - a + 1) * (d - c + 1) - 1;
    return total;
 
# Driver code
if __name__ == "__main__" :
 
    R = 4; C = 5; M = 2;
    print(Square(R, C, M));
     
# This code is contributed by Ryuga


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
// Function to return the number
// of squares that the king can
// reach in the given number of moves
static int Square(int row, int column,
                            int moves)
{
    int a = 0, b = 0, c = 0,
        d = 0, total = 0;
 
    // Calculate initial and final coordinates
    a = row - moves;
    b = row + moves;
    c = column - moves;
    d = column + moves;
 
    // Since chessboard is of size 8X8
    // so if any coordinate is less
    // than 1 or greater than 8 make
    // it 1 or 8.
    if (a < 1)
        a = 1;
    if (c < 1)
        c = 1;
    if (b > 8)
        b = 8;
    if (d > 8)
        d = 8;
 
    // Calculate total positions
    total = (b - a + 1) * (d - c + 1) - 1;
    return total;
}
 
// Driver code
public static void Main()
{
    int R = 4, C = 5, M = 2;
    Console.Write(Square(R, C, M));
}
}
 
// this code is contributed by Ita_c.


PHP
 8)
        $b = 8;
    if ($d > 8)
        $d = 8;
 
    // Calculate total positions
    $total = ($b - $a + 1) *
             ($d - $c + 1) - 1;
    return $total;
}
 
// Driver code
$R = 4; $C = 5; $M = 2;
echo Square($R, $C, $M);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:
24