📌  相关文章
📜  使用 M 种颜色填充 N 个位置的方法,以便恰好有 K 对相邻的不同颜色

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

给定三个整数NMK 。任务是找到使用M种颜色填充N 个位置的方法的数量,以便恰好有K对不同的相邻颜色。

例子:

方法:我们可以使用带有记忆化的动态规划来解决上述问题。有N 个位置要填充,因此递归函数将由两个调用组成,一个是用相同的颜色填充下一个位置,另一个是用不同的颜色填充。因此,递归调用将是:

  • countWays(index + 1, cnt) ,如果下一个索引用相同的颜色填充。
  • (m – 1) * countWays(index + 1, cnt + 1) ,如果下一个索引用不同的颜色填充。路数乘以(m – 1)

基本情况将是:

  • 如果index = n ,则检查cnt的值。如果cnt = K那么这是一种可能的方式,因此返回1 ,否则返回0
  • 为避免重复调用,将返回值记忆在二维数组中,如果再次使用相同参数进行递归调用,则返回该值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define max 4
 
// Recursive function to find the required number of ways
int countWays(int index, int cnt, int dp[][max], int n, int m, int k)
{
 
    // When all positions are filled
    if (index == n) {
 
        // If adjacent pairs are exactly K
        if (cnt == k)
            return 1;
        else
            return 0;
    }
 
    // If already calculated
    if (dp[index][cnt] != -1)
        return dp[index][cnt];
 
    int ans = 0;
 
    // Next position filled with same color
    ans += countWays(index + 1, cnt, dp, n, m, k);
 
    // Next position filled with different color
    // So there can be m-1 different colors
    ans += (m - 1) * countWays(index + 1, cnt + 1, dp, n, m, k);
 
    return dp[index][cnt] = ans;
}
 
// Driver Code
int main()
{
    int n = 3, m = 3, k = 2;
    int dp[n + 1][max];
    memset(dp, -1, sizeof dp);
 
    cout << m * countWays(1, 0, dp, n, m, k);
}


Java
//Java implementation of the approach
class solution
{
static final int  max=4;
  
// Recursive function to find the required number of ways
static int countWays(int index, int cnt, int dp[][], int n, int m, int k)
{
  
    // When all positions are filled
    if (index == n) {
  
        // If adjacent pairs are exactly K
        if (cnt == k)
            return 1;
        else
            return 0;
    }
  
    // If already calculated
    if (dp[index][cnt] != -1)
        return dp[index][cnt];
  
    int ans = 0;
  
    // Next position filled with same color
    ans += countWays(index + 1, cnt, dp, n, m, k);
  
    // Next position filled with different color
    // So there can be m-1 different colors
    ans += (m - 1) * countWays(index + 1, cnt + 1, dp, n, m, k);
  
    return dp[index][cnt] = ans;
}
  
// Driver Code
public static void main(String args[])
{
    int n = 3, m = 3, k = 2;
    int dp[][]= new int [n + 1][max];
    for(int i=0;i


Python 3
# Python 3 implementation of the approach
 
max = 4
 
# Recursive function to find the
# required number of ways
def countWays(index, cnt, dp, n, m, k):
 
    # When all positions are filled
    if (index == n) :
 
        # If adjacent pairs are exactly K
        if (cnt == k):
            return 1
        else:
            return 0
 
    # If already calculated
    if (dp[index][cnt] != -1):
        return dp[index][cnt]
 
    ans = 0
 
    # Next position filled with same color
    ans += countWays(index + 1, cnt, dp, n, m, k)
 
    # Next position filled with different color
    # So there can be m-1 different colors
    ans += (m - 1) * countWays(index + 1,
                               cnt + 1, dp, n, m, k)
 
    dp[index][cnt] = ans
    return dp[index][cnt]
 
# Driver Code
if __name__ == "__main__":
     
    n = 3
    m = 3
    k = 2
    dp = [[-1 for x in range(n + 1)]
              for y in range(max)]
 
    print(m * countWays(1, 0, dp, n, m, k))
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
 
using System;
 
class solution
{
static int max=4;
 
// Recursive function to find the required number of ways
static int countWays(int index, int cnt, int [,]dp, int n, int m, int k)
{
 
    // When all positions are filled
    if (index == n) {
 
        // If adjacent pairs are exactly K
        if (cnt == k)
            return 1;
        else
            return 0;
    }
 
    // If already calculated
    if (dp[index,cnt] != -1)
        return dp[index,cnt];
 
    int ans = 0;
 
    // Next position filled with same color
    ans += countWays(index + 1, cnt, dp, n, m, k);
 
    // Next position filled with different color
    // So there can be m-1 different colors
    ans += (m - 1) * countWays(index + 1, cnt + 1, dp, n, m, k);
 
    return dp[index,cnt] = ans;
}
 
// Driver Code
public static void Main()
{
    int n = 3, m = 3, k = 2;
    int [,]dp= new int [n + 1,max];
    for(int i=0;i


PHP


Javascript


输出:
12

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程