📜  国际象棋骑士在移动键盘上的 N 步移动中可以形成的不同数字的计数

📅  最后修改于: 2022-05-13 01:56:09.933000             🧑  作者: Mango

国际象棋骑士在移动键盘上的 N 步移动中可以形成的不同数字的计数

给定一个整数N和一个放置在移动键盘中的国际象棋骑士。任务是计算可以由国际象棋骑士用 N 步组成的不同 N 位数字的总数。因为答案可能非常大,所以给出答案模10 9 + 7的值。

注意:每走一步棋马可以水平移动2个单位,垂直移动1个单位,或者垂直移动2个单位,水平移动1个单位。

演示移动键盘如图所示,其中“*”和“#”不被视为数字的一部分。

例子:

方法:这个想法是为每个单元格找到可以从给定单元格到达的可能单元格,并将所有单元格相加以找到答案。请按照以下步骤解决问题:

  • 初始化向量v[10, 1]temp[10]
  • 使用变量i遍历范围[1, N)并执行以下任务:
    • temp[]中找到所有单元格的值,然后将它们存储在向量v[] 中。
  • 将变量sum初始化为0以存储答案。
  • 使用变量i遍历范围[0, 10)并执行以下任务:
    • v[i]的值添加到变量sum中。
  • 执行上述步骤后,打印sum的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the total number of ways
int knightCalling(int N)
{
    int mod = 1000000007;
 
    // Base Case
    if (N == 1)
        return 10;
    vector v(10, 1);
    vector temp(10);
 
    // No cell can be reached from a
    // cell with value 5
    v[5] = 0;
    for (int i = 1; i < N; i++)
    {
       
        // Find the possible values from all cells
        temp[0] = (v[4] + v[6]) % mod;
        temp[1] = (v[6] + v[8]) % mod;
        temp[2] = (v[7] + v[9]) % mod;
        temp[3] = (v[4] + v[8]) % mod;
        temp[4] = (v[0] + v[3] + v[9]) % mod;
        temp[6] = (v[0] + v[1] + v[7]) % mod;
        temp[7] = (v[2] + v[6]) % mod;
        temp[8] = (v[1] + v[3]) % mod;
        temp[9] = (v[2] + v[4]) % mod;
 
        // Store them
        for (int j = 0; j < 10; j++)
            v[j] = temp[i];
    }
 
    // Find the answer
    int sum = 0;
    for (int i = 0; i < 10; i++)
        sum = (sum + v[i]) % mod;
    return sum;
}
 
// Driver Code
int main()
{
    int N = 2;
    cout << knightCalling(N);
}
 
// This code is contributed by Samim Hossain Mondal.


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the total number of ways
static int knightCalling(int N)
{
    int mod = 1000000007;
 
    // Base Case
    if (N == 1)
        return 10;
    int []v = new int[10];
    int []temp = new int[10];
    Arrays.fill(v, 1);
 
    // No cell can be reached from a
    // cell with value 5
    v[5] = 0;
    for (int i = 1; i < N; i++)
    {
       
        // Find the possible values from all cells
        temp[0] = (v[4] + v[6]) % mod;
        temp[1] = (v[6] + v[8]) % mod;
        temp[2] = (v[7] + v[9]) % mod;
        temp[3] = (v[4] + v[8]) % mod;
        temp[4] = (v[0] + v[3] + v[9]) % mod;
        temp[6] = (v[0] + v[1] + v[7]) % mod;
        temp[7] = (v[2] + v[6]) % mod;
        temp[8] = (v[1] + v[3]) % mod;
        temp[9] = (v[2] + v[4]) % mod;
 
        // Store them
        for (int j = 0; j < 10; j++)
            v[i] = temp[i];
    }
 
    // Find the answer
    int sum = 0;
    for (int i = 0; i < 10; i++)
        sum = (sum + v[i]) % mod;
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2;
    System.out.print(knightCalling(N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3  program for the above approach
 
# Function to find the total number of ways
def knightCalling(N):
 
    mod = 1000000007
 
    # Base Case
    if (N == 1):
        return 10
    v = [1]*10
    temp = [0]*10
 
    # No cell can be reached from a
    # cell with value 5
    v[5] = 0
    for i in range(1, N):
       
        # Find the possible values from all cells
        temp[0] = (v[4] + v[6]) % mod
        temp[1] = (v[6] + v[8]) % mod
        temp[2] = (v[7] + v[9]) % mod
        temp[3] = (v[4] + v[8]) % mod
        temp[4] = (v[0] + v[3] + v[9]) % mod
        temp[6] = (v[0] + v[1] + v[7]) % mod
        temp[7] = (v[2] + v[6]) % mod
        temp[8] = (v[1] + v[3]) % mod
        temp[9] = (v[2] + v[4]) % mod
 
        # Store them
        for j in range(10):
            v[j] = temp[j]
 
    # Find the answer
    sum = 0
    for i in range(10):
        sum = (sum + v[i]) % mod
    return sum
 
# Driver Code
if __name__ == "__main__":
 
    N = 2
    print(knightCalling(N))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to find the total number of ways
  static int knightCalling(int N)
  {
    int mod = 1000000007;
 
    // Base Case
    if (N == 1)
      return 10;
    int []v = new int[10];
    int []temp = new int[10];
    for(int i = 0; i < 10; i++) {
      v[i] = 1;
    }
 
    // No cell can be reached from a
    // cell with value 5
    v[5] = 0;
    for (int i = 1; i < N; i++)
    {
 
      // Find the possible values from all cells
      temp[0] = (v[4] + v[6]) % mod;
      temp[1] = (v[6] + v[8]) % mod;
      temp[2] = (v[7] + v[9]) % mod;
      temp[3] = (v[4] + v[8]) % mod;
      temp[4] = (v[0] + v[3] + v[9]) % mod;
      temp[6] = (v[0] + v[1] + v[7]) % mod;
      temp[7] = (v[2] + v[6]) % mod;
      temp[8] = (v[1] + v[3]) % mod;
      temp[9] = (v[2] + v[4]) % mod;
 
      // Store them
      for (int j = 0; j < 10; j++)
        v[j] = temp[i];
    }
 
    // Find the answer
    int sum = 0;
    for (int i = 0; i < 10; i++)
      sum = (sum + v[i]) % mod;
    return sum;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 2;
    Console.Write(knightCalling(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
20

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