📜  计算棋盘中女王可以访问但国王没有访问的位置

📅  最后修改于: 2021-10-26 05:20:22             🧑  作者: Mango

给定两个整数NM表示棋盘的尺寸,以及两个整数XY表示国王的位置,即单元格(X, Y) 。任务是找到女王可以访问的单元格数量,如果它被国王取代,国王不会访问这些单元格。国王访问他所有相邻的单元格,王后可以对角、水平和垂直移动。

例子:

方法:思路是先求女王可以访问的位置总数。然后找出国王可以访问的位置总数。只能由女王访问的单元格数量将是国王可以访问的单元格数量减去女王可以访问的单元格数量。请按照以下步骤解决问题:

  • QueenMoves初始化为 0 并计算皇后的总移动如下:
  • kingMoves初始化为 0 并按如下方式计算 King 的移动:
  • 打印在上述步骤中计算出的女王和国王之间的绝对差作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the number of cells
// only visited by the queen
int Moves_Calculator(int x, int y,
                     int row, int col)
{
     
    // Find all the moves
    int total_moves = 0;
     
    // Find all moves for x + 1, y + 1
    if ((row - x) > 0 && (col - y) > 0)
        total_moves += min((row - x),
                           (col - y));
 
    // Find all moves for x - 1, y - 1
    if ((y - 1) > 0 && (x - 1) > 0)
        total_moves += min((y - 1),
                           (x - 1));
 
    // Find all moves for x - 1, y + 1
    if ((x - 1) > 0 && (col - y) > 0)
        total_moves += min((x - 1),
                         (col - y));
 
    // Find all moves for x + 1, y - 1
    if ((row - x) > 0 && (y - 1) > 0)
        total_moves += min((row - x),
                             (y - 1));
 
    total_moves += (row - 1) + (col - 1);
 
    // Find all squares visited by King
    // x + 1, in same row
    int king_moves = 0;
     
    if (x + 1 <= row)
        king_moves += 1;
 
    // x - 1, in same row
    if (x - 1 > 0)
        king_moves += 1;
 
    // y + 1, in same column
    if (y + 1 <= col)
        king_moves += 1;
 
    // y - 1, in same column
    if (y - 1 > 0)
        king_moves += 1;
 
    if (x + 1 <= row && y + 1 <= col)
        king_moves += 1;
 
    if (x + 1 <= row && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y + 1 <= col)
        king_moves += 1;
 
    // Return answer
    return total_moves - king_moves;
}
 
// Driver Code
int main()
{
     
    // Dimension of Board
    int n = 8, m = 8;
     
    // Position of Cell
    int x = 1, y = 1;
     
    // Function Call
    cout << (Moves_Calculator(x, y, m, n));
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to print the number of cells
// only visited by the queen
static int Moves_Calculator(int x, int y,
                            int row, int col)
{
     
    // Find all the moves
    int total_moves = 0;
 
    // Find all moves for x + 1, y + 1
    if ((row - x) > 0 && (col - y) > 0)
        total_moves += Math.min((row - x),
                                (col - y));
 
    // Find all moves for x - 1, y - 1
    if ((y - 1) > 0 && (x - 1) > 0)
        total_moves += Math.min((y - 1),
                                (x - 1));
 
    // Find all moves for x - 1, y + 1
    if ((x - 1) > 0 && (col - y) > 0)
        total_moves += Math.min((x - 1),
                              (col - y));
 
    // Find all moves for x + 1, y - 1
    if ((row - x) > 0 && (y - 1) > 0)
        total_moves += Math.min((row - x),
                                  (y - 1));
 
    total_moves += (row - 1) + (col - 1);
 
    // Find all squares visited by King
    // x + 1, in same row
    int king_moves = 0;
    if (x + 1 <= row)
        king_moves += 1;
 
    // x - 1, in same row
    if (x - 1 > 0)
        king_moves += 1;
 
    // y + 1, in same column
    if (y + 1 <= col)
        king_moves += 1;
 
    // y - 1, in same column
    if (y - 1 > 0)
        king_moves += 1;
 
    if (x + 1 <= row && y + 1 <= col)
        king_moves += 1;
 
    if (x + 1 <= row && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y + 1 <= col)
        king_moves += 1;
 
    // Return answer
    return total_moves - king_moves;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Dimension of Board
    int n = 8, m = 8;
 
    // Position of Cell
    int x = 1, y = 1;
 
    // Function Call
    System.out.println(Moves_Calculator(x, y, m, n));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to print the number of cells
# only visited by the queen
def Moves_Calculator(x, y, row, col):
 
    # Find all the moves
    total_moves = 0
     
    # Find all moves for x + 1, y + 1
    if (row - x) > 0 and (col - y) > 0:
        total_moves += min((row - x), (col - y))
 
    # Find all moves for x - 1, y - 1
    if (y - 1) > 0 and (x - 1) > 0:
        total_moves += min((y - 1), (x - 1))
 
    # Find all moves for x - 1, y + 1
    if (x - 1) > 0 and (col - y) > 0:
        total_moves += min((x - 1), (col - y))
 
    # Find all moves for x + 1, y - 1
    if (row - x) > 0 and (y - 1) > 0:
        total_moves += min((row - x), (y - 1))
 
    total_moves += (row - 1) + (col - 1)
 
    # Find all squares visited by King
    # x + 1, in same row
    king_moves = 0
    if x + 1 <= m:
        king_moves += 1
 
    # x - 1, in same row
    if x - 1 > 0:
        king_moves += 1
 
    # y + 1, in same column
    if y + 1 <= n:
        king_moves += 1
 
    # y - 1, in same column
    if y - 1 > 0:
        king_moves += 1
 
    if x + 1 <= m and y + 1 <= n:
        king_moves += 1
 
    if x + 1 <= m and y - 1 > 0:
        king_moves += 1
 
    if x - 1 > 0 and y - 1 > 0:
        king_moves += 1
 
    if x - 1 > 0 and y + 1 <= n:
        king_moves += 1
 
    # Return answer
    return total_moves - king_moves
 
 
# Driver Code
if __name__ == '__main__':
 
    # Dimension of Board
    n, m = 8, 8
     
    # Position of Cell
    x, y = 1, 1
     
    # Function Call
    print(Moves_Calculator(x, y, m, n))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print the number of cells
// only visited by the queen
static int Moves_Calculator(int x, int y,
                            int row, int col)
{
     
    // Find all the moves
    int total_moves = 0;
 
    // Find all moves for x + 1, y + 1
    if ((row - x) > 0 && (col - y) > 0)
        total_moves += Math.Min((row - x),
                                (col - y));
 
    // Find all moves for x - 1, y - 1
    if ((y - 1) > 0 && (x - 1) > 0)
        total_moves += Math.Min((y - 1),
                                (x - 1));
 
    // Find all moves for x - 1, y + 1
    if ((x - 1) > 0 && (col - y) > 0)
        total_moves += Math.Min((x - 1),
                              (col - y));
 
    // Find all moves for x + 1, y - 1
    if ((row - x) > 0 && (y - 1) > 0)
        total_moves += Math.Min((row - x),
                                  (y - 1));
 
    total_moves += (row - 1) + (col - 1);
 
    // Find all squares visited by King
    // x + 1, in same row
    int king_moves = 0;
    if (x + 1 <= row)
        king_moves += 1;
 
    // x - 1, in same row
    if (x - 1 > 0)
        king_moves += 1;
 
    // y + 1, in same column
    if (y + 1 <= col)
        king_moves += 1;
 
    // y - 1, in same column
    if (y - 1 > 0)
        king_moves += 1;
 
    if (x + 1 <= row && y + 1 <= col)
        king_moves += 1;
 
    if (x + 1 <= row && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y - 1 > 0)
        king_moves += 1;
 
    if (x - 1 > 0 && y + 1 <= col)
        king_moves += 1;
 
    // Return answer
    return total_moves - king_moves;
}
 
// Driver Code
public static void Main()
{
 
    // Dimension of Board
    int n = 8, m = 8;
 
    // Position of Cell
    int x = 1, y = 1;
 
    // Function Call
    Console.WriteLine(Moves_Calculator(x, y, m, n));
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
18

时间复杂度: O(1)
辅助空间: O(1)