📌  相关文章
📜  计算在棋盘中放置以 L 形移动的骑士的方法

📅  最后修改于: 2021-09-05 08:59:08             🧑  作者: Mango

给定两个整数NM ,表示棋盘的尺寸。任务是计算将黑白骑士放置在 N * M 棋盘上的方法,以使它们不会相互攻击?骑士必须被放置在不同的方格上。骑士可以水平移动两个格子和垂直一个格子(L 形),或垂直移动两个格子和水平一个格子(L 形)。如果一个人可以一步到达对方,骑士就会互相攻击。
例子:

Input: N=2, M=2
Output: 12
Explanation: 
We can place a black and a white knight in 12 possible 
ways such that none of them attracts each other.

Input: N=2, M=3
Output: 26

天真的方法:

  • 朴素的方法是为每个单元计算骑士可以放置的方式的数量,以便双方可以互相攻击,然后从总数中减去计数。
  • 总安排可以计算为:
Total arrangements = M * N * (M * N - 1)
  • 因为对于每个单元格,我们可以将其他骑士放置在 (M * N – 1) 个单元格处,并且总共 M * N 个单元格将在那里。
C++
// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include 
using namespace std;
 
// Function returns the count
// of arrangments
long long Solve(int n, int m)
{
    int X_axis[]{ -2, -1, 1, 2};
    int Y_axis[]{ 1, 2, 2, 1 };
     
    long long ret = 0;
     
    for (int i = 0; i < m; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            for (int k = 0; k < 4; ++k)
            {
                int x = i + X_axis[k],
                    y = j + Y_axis[k];
                 
                if (x >= 0 && x < m
                    && y >= 0 && y < n)
                    ++ret;
            }
        }
    }
     
    long long Total = m * n;
    Total = Total * (Total - 1) / 2;
     
    return 2 * (Total - ret);
}
// Driver code
int main()
{
     
    int N = 2, M = 3;
     
    cout << Solve(N, M) << endl;
    return 0;
}


Java
// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function returns the count
// of arrangments
static long Solve(int n, int m)
{
    int X_axis[] = { -2, -1, 1, 2};
    int Y_axis[] = { 1, 2, 2, 1 };
     
    long ret = 0;
     
    for(int i = 0; i < m; ++i)
    {
       for(int j = 0; j < n; ++j)
       {
          for(int k = 0; k < 4; ++k)
          {
             int x = i + X_axis[k];
             int y = j + Y_axis[k];
                 
             if (x >= 0 && x < m &&
                 y >= 0 && y < n)
                 ++ret;
          }
       }
    }
 
    long Total = m * n;
    Total = Total * (Total - 1) / 2;
    return 2 * (Total - ret);
}
     
// Driver code
public static void main(String[] args)
{
    int N = 2, M = 3;
    System.out.println(Solve(N, M));
}
}
 
// This code is contributed by coder001


Python3
# Python3 program to count arrangements
# of two knight so that they do not
# attack each other
 
# Function returns the count
# of arrangments
def Solve(n, m):
     
    X_axis = []
    X_axis = [ -2, -1, 1, 2 ]
    Y_axis = []
    Y_axis = [ 1, 2, 2, 1 ]
     
    ret = 0
     
    for i in range(m):
        for j in range(n):
            for k in range(4):
                 
                x = i + X_axis[k]
                y = j + Y_axis[k]
                 
                if (x >= 0 and x < m and
                    y >= 0 and y < n):
                    ret += 1
                     
    Total = m * n
    Total = Total * (Total - 1) // 2
     
    return 2 * (Total - ret)
 
# Driver code
N = 2
M = 3
     
print(Solve(N, M))
 
# This code is contributed by sanjoy_62


C#
// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
class GFG{
     
// Function returns the count
// of arrangments
static long Solve(int n, int m)
{
    int []X_axis = { -2, -1, 1, 2};
    int []Y_axis = { 1, 2, 2, 1 };
     
    long ret = 0;
     
    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            for(int k = 0; k < 4; ++k)
            {
                int x = i + X_axis[k];
                int y = j + Y_axis[k];
                     
                if (x >= 0 && x < m &&
                    y >= 0 && y < n)
                    ++ret;
            }
        }
    }
 
    long Total = m * n;
    Total = Total * (Total - 1) / 2;
    return 2 * (Total - ret);
}
     
// Driver code
public static void Main(String[] args)
{
    int N = 2, M = 3;
    Console.Write(Solve(N, M));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include 
using namespace std;
 
// Function returns the count
// of arrangments
long long Solve(int N, int M)
{
 
    // Total arrangements
    int ans = (N * M - 1) * N * M;
     
    if (N >= 1 && M >= 2)
    {
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
         
    }
    if (N >= 2 && M >= 1)
    {
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
     
    return ans;
}
 
// Driver code
int main()
{
     
    int N = 2, M = 3;
     
    cout << Solve(N, M) << endl;
    return 0;
}


Java
// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
    // Total arrangements
    int ans = (N * M - 1) * N * M;
     
    if (N >= 1 && M >= 2)
    {
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
         
    }
    if (N >= 2 && M >= 1)
    {
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
     
    return ans;
}
     
// Driver code
public static void main(String[] args)
{
    int N = 2, M = 3;
    System.out.println(Solve(N, M));
}
}
 
// This code is contributed by coder001


Python3
# Python3 program to count arrangements
# of two knight so that they do not
# attack each other.
 
# Function returns the count
# of arrangments
def Solve(N, M):
 
    # Total arrangements
    ans = (N * M - 1) * N * M
     
    if (N >= 1 and M >= 2):
         
        # Attacks possible in one horizontal
        # and two vertical steps
        ans -= (4 * (N - 1) * (M - 2))
         
    if (N >= 2 and M >= 1):
         
        # Attacks possible in Two horizontal
        # and one vertical steps
        ans -= (4 * (N - 2) * (M - 1))
     
    return ans
 
# Driver code
N = 2
M = 3
     
print(Solve(N, M))
 
# This code is contributed by sanjoy_62


C#
// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
 
class GFG{
     
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
     
    // Total arrangements
    int ans = (N * M - 1) * N * M;
         
    if (N >= 1 && M >= 2)
    {
         
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
    }
    if (N >= 2 && M >= 1)
    {
         
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
    return ans;
}
         
// Driver code
static public void Main ()
{
    int N = 2, M = 3;
     
    Console.Write(Solve(N, M));
}
}
 
// This code is contributed by ShubhamCoder


Javascript


输出:
26

时间复杂度: O(N * M)
空间复杂度: O(1)。
有效的方法:

  • 假设棋盘有 N 行 M 列。现在首先考虑骑士在水平方向移动 2 步,在垂直方向移动 1 步。所以如果我们在 (i, j) 移动之后我们可以到达 (i+2, j+ 1), (i+2, j-1), (i-2, j+1), (i-2, j-1)。为了让 (i+2) 在板内,我们可以将位置从 0 到 N -3 即我们必须留下最后两行,否则 (i+2) 将超出板。类似地,如果 2 到 N,则 (i-2) 范围可能。
  • 同样,对于 (j+1) 范围将是 0 到 N-2,对于 (j+1) 范围将是 1 到 M-1,即在每种情况下都少一列。
  • 因此,在这种情况下,攻击可能等于4 * (N – 2) * (M – 1)
  • 类似地,如果我们考虑垂直两步和水平一步,我们将减少一排和两排,以便两个骑士可以互相攻击。
  • 我们将从总安排中减去此安排,即M * N * (M * N – 1)

C++

// C++ program to count arrangements
// of two knight so that they do not
// attack each other.
#include 
using namespace std;
 
// Function returns the count
// of arrangments
long long Solve(int N, int M)
{
 
    // Total arrangements
    int ans = (N * M - 1) * N * M;
     
    if (N >= 1 && M >= 2)
    {
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
         
    }
    if (N >= 2 && M >= 1)
    {
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
     
    return ans;
}
 
// Driver code
int main()
{
     
    int N = 2, M = 3;
     
    cout << Solve(N, M) << endl;
    return 0;
}

Java

// Java program to count arrangements
// of two knight so that they do not
// attack each other.
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
    // Total arrangements
    int ans = (N * M - 1) * N * M;
     
    if (N >= 1 && M >= 2)
    {
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
         
    }
    if (N >= 2 && M >= 1)
    {
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
     
    return ans;
}
     
// Driver code
public static void main(String[] args)
{
    int N = 2, M = 3;
    System.out.println(Solve(N, M));
}
}
 
// This code is contributed by coder001

蟒蛇3

# Python3 program to count arrangements
# of two knight so that they do not
# attack each other.
 
# Function returns the count
# of arrangments
def Solve(N, M):
 
    # Total arrangements
    ans = (N * M - 1) * N * M
     
    if (N >= 1 and M >= 2):
         
        # Attacks possible in one horizontal
        # and two vertical steps
        ans -= (4 * (N - 1) * (M - 2))
         
    if (N >= 2 and M >= 1):
         
        # Attacks possible in Two horizontal
        # and one vertical steps
        ans -= (4 * (N - 2) * (M - 1))
     
    return ans
 
# Driver code
N = 2
M = 3
     
print(Solve(N, M))
 
# This code is contributed by sanjoy_62

C#

// C# program to count arrangements
// of two knight so that they do not
// attack each other.
using System;
 
class GFG{
     
// Function returns the count
// of arrangments
static long Solve(int N, int M)
{
     
    // Total arrangements
    int ans = (N * M - 1) * N * M;
         
    if (N >= 1 && M >= 2)
    {
         
        // Attacks possible in one horizontal
        // and two vertical steps
        ans -= (4 * (N - 1) * (M - 2));
    }
    if (N >= 2 && M >= 1)
    {
         
        // Attacks possible in Two horizontal
        // and one vertical steps
        ans -= (4 * (N - 2) * (M - 1));
    }
    return ans;
}
         
// Driver code
static public void Main ()
{
    int N = 2, M = 3;
     
    Console.Write(Solve(N, M));
}
}
 
// This code is contributed by ShubhamCoder

Javascript


输出:

26

时间复杂度: O(1)。
空间复杂度: O(1)。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live