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

📅  最后修改于: 2021-05-05 00:51:28             🧑  作者: 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
  • 为避免重复调用,请在2-D数组中记住返回的值,如果再次执行具有相同参数的递归调用,则返回此值。

下面是上述方法的实现:

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


输出:
12