📜  从国际象棋棋盘保存

📅  最后修改于: 2021-04-26 07:44:01             🧑  作者: Mango

您会得到一个8 * 8的国际象棋棋盘。与国际象棋棋盘一起,还有一个主教被放置在棋盘上,其位置是已知的。 Bishop的位置以两位整数的形式给出,其中两位均大于0且小于9(例如67表示第6行第7列)。现在您的任务是找到将棋子安全地放置在棋盘上的多种方法。
例子:

Input : Bishop's Position = 11
Output : Safe Positions = 56

Input : Bishop's Position = 44 
Output : Safe Positions = 50

蛮力法:一种基本方法是遍历棋盘上的所有64个可能位置,并检查该位置是否安全。这种方法将需要更多时间。
更好的方法:我们知道Bishop的移动是对角线的,因此Bishop从棋盘上的任何位置都可以在两个对角线的两个方向上移动。因此,所有不位于给定主教对角线运动方式上的位置都是安全位置。
现在,我们的任务是从Bishop的位置找到所有可能的四个方向上的最大长度。

->主教可以从任何位置移向(11,18,81,88)的四个角。因此,我们将尝试找到主教可以朝这些角移动的最大距离。
然后让主教的位置为ij:

  1. 朝向11 = min的距离(mod(1-i),mod(1-j))。
  2. 朝向18的距离=最小值(mod(1-i),mod(8-j))。
  3. 朝向81 = min的距离(mod(8-i),mod(1-j))。
  4. 朝向88 = min的距离(mod(8-i),mod(8-j))。

除了这四个以外,Bishop所处的一个位置也不安全,因此不安全位置的总数为上述结果的总和+1。安全位置的总数为64-(sum + 1)。

C++
// CPP program to find total safe position
// to place your Bishop
#include
using namespace std;
 
// function to calc total safe position
int calcSafe(int pos)
{
    // i,j denotes row and column of position of bishop
    int j = pos % 10;
    int i = pos /10;
 
    // calc distance in four direction
    int dis_11 = min ( abs(1-i), abs (1-j));
    int dis_18 = min ( abs(1-i), abs (8-j));
    int dis_81 = min ( abs(8-i), abs (1-j));
    int dis_88 = min ( abs(8-i), abs (8-j));
 
    // calc total sum of distance  + 1 for unsafe positions
    int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1;
 
    // return total safe positions
    return (64- sum);
}
 
// driver function
int main()
{
    int pos = 34;
    cout << "Safe Positions = " << calcSafe(pos);
    return 0;
}


Java
// Java program to find total safe position
// to place your Bishop
class GFG
{
     
    // function to calc total safe position
    static int calcSafe(int pos)
    {
         
        // i,j denotes row and column of position of bishop
        int j = pos % 10;
        int i = pos /10;
     
        // calc distance in four direction
        int dis_11 = Math.min ( Math.abs(1-i), Math.abs (1-j));
        int dis_18 = Math.min ( Math.abs(1-i), Math.abs (8-j));
        int dis_81 = Math.min ( Math.abs(8-i), Math.abs (1-j));
        int dis_88 = Math.min ( Math.abs(8-i), Math.abs (8-j));
     
        // calc total sum of distance + 1 for unsafe positions
        int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1;
     
        // return total safe positions
        return (64- sum);
    }
         
    // Driver function
    public static void main (String[] args)
    {
        int pos = 34;
         
        System.out.print("Safe Positions = "+calcSafe(pos));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# python program to find total safe
# position to place your Bishop
import math
 
# function to calc total safe position
def calcSafe(pos):
     
    # i,j denotes row and column of
    # position of bishop
    j = pos % 10
    i = pos /10
 
    # calc distance in four direction
    dis_11 = min ( abs(1-i), abs (1-j))
    dis_18 = min ( abs(1-i), abs (8-j))
    dis_81 = min ( abs(8-i), abs (1-j))
    dis_88 = min ( abs(8-i), abs (8-j))
 
    # calc total sum of distance + 1
    # for unsafe positions
    sum = (dis_11 + dis_18 + dis_81
                        + dis_88 + 1)
 
    # return total safe positions
    return (64- sum)
 
 
# driver function
pos = 34
print("Safe Positions = " ,
            math.ceil(calcSafe(pos)))
 
# This code is contributed by Sam007


C#
// Program to find the total safe
// positions to place your Bishop
using System;
 
class GFG {
 
    // function to calc total safe position
    static int calcSafe(int pos)
    {
 
        // i, j denotes row and column of
        // position of bishop
        int j = pos % 10;
        int i = pos / 10;
 
        // calc distance in four direction
        int dis_11 = Math.Min(Math.Abs(1 - i), Math.Abs(1 - j));
        int dis_18 = Math.Min(Math.Abs(1 - i), Math.Abs(8 - j));
        int dis_81 = Math.Min(Math.Abs(8 - i), Math.Abs(1 - j));
        int dis_88 = Math.Min(Math.Abs(8 - i), Math.Abs(8 - j));
 
        // calc total sum of distance + 1
        // for unsafe positions
        int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1;
 
        // return total safe positions
        return (64 - sum);
    }
 
    // Driver function
    public static void Main()
    {
        int pos = 34;
 
        Console.WriteLine("Safe Positions = " + calcSafe(pos));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

Safe Positions = 52