📜  在2 * N矩阵中精确制作C分量的方法数量

📅  最后修改于: 2021-04-22 02:05:36             🧑  作者: Mango

给定两个正整数NC。有一个2 * N矩阵,其中矩阵的每个单元格都可以用0或1进行着色。任务是寻找多种方式,形成的2 * N矩阵恰好具有c个成分。

例子:

方法:解决此问题的想法是使用动态编程。构造一个4D DP矩阵(N+1) * (C+1) * 2 * 2大小,其中N是列数,C是组件数,2 * 2维是最后一列的排列。最后一列可以有四个不同的组合。它们是00、01、10、11。

当最后一列的排列为row1,row2时,矩阵dp [i] [j] [row1] [row2]的每个单元给出在i列中具有j个分量的方式的数量。如果已经评估了当前子问题,即dp [column] [component] [row1] [row2]!= -1,则使用此结果,否则递归计算该值。

  • 基本情况:当列数大于N时,返回0。如果列数等于N,则答案取决于组件数。如果分量等于C,则返回1,否则返回0。
  • 情况1 :当前一列的行中具有相同的颜色({0,0}或{1,1})时。
    在这种情况下,如果当前列在行中具有不同的值({0,1}或{1,0}),则组件将增加1。如果当前列的颜色相反,但行中的值相同,则分量也将增加1。也就是说,当前一列具有{0,0}时,当前列具有{1,1}。或当前列为{1,1}时当前列为{0,0}。当当前列与上一列具有相同的组合时,组件将保持不变。
  • 情况2 :当前一列的行中具有不同的颜色({0,1}或{1,0})时。
    在这种情况下,如果当前列与上一列具有完全相反的组合,则组件将增加2。那就是当当前列是{1,0}而前一列是{0,1}时。或当前列为{0,1},前一列为{1,0}。在所有其他情况下,组件保持不变。

下面是上述方法的实现:

C++
// C++ implementation to find the
// number of ways to make exactly
// C components in a 2 * N matrix
  
#include 
using namespace std;
  
// row1 and row2 are one
// when both are same colored
int n, k;
int dp[1024][2048][2][2];
  
// Function to find the number of
// ways to make exactly C components
// in a 2 * N matrix
int Ways(int col, int comp,
         int row1, int row2)
{
  
    // if No of components
    // at any stage exceeds
    // the given number
    // then base case
    if (comp > k)
        return 0;
  
    if (col > n) {
        if (comp == k)
            return 1;
        else
            return 0;
    }
  
    // Condition to check
    // if already visited
    if (dp[col][comp][row1][row2] != -1)
        return dp[col][comp][row1][row2];
  
    // if not visited previously
    else {
        int ans = 0;
  
        // At the first column
        if (col == 1) {
  
            // color {white, white} or
            // {black, black}
            ans
                = (ans
                   + Ways(col + 1, comp + 1, 0, 0)
                   + Ways(col + 1, comp + 1, 1, 1));
  
            // Color {white, black} or
            // {black, white}
            ans
                = (ans
                   + Ways(col + 1, comp + 2, 0, 1)
                   + Ways(col + 1, comp + 2, 1, 0));
        }
        else {
  
            // If previous both
            // rows have same color
            if ((row1 && row2)
                || (!row1 && !row2)) {
  
                // Fill with {same, same} and
                // {white, black} and {black, white}
                ans = (((ans
                         + Ways(col + 1, comp + 1, 0, 0))
                        + Ways(col + 1, comp + 1, 1, 0))
                       + Ways(col + 1, comp + 1, 0, 1));
  
                // Fill with same without
                // increase in component
                // as it has been
                // counted previously
                ans = (ans
                       + Ways(col + 1, comp, 1, 1));
            }
  
            // When previous rows
            // had {white, black}
            if (row1 && !row2) {
                ans = (((ans
                         + Ways(col + 1, comp, 0, 0))
                        + Ways(col + 1, comp, 1, 1))
                       + Ways(col + 1, comp, 1, 0));
                ans = (ans
                       + Ways(col + 1, comp + 2, 0, 1));
            }
  
            // When previous rows
            // had {black, white}
            if (!row1 && row2) {
                ans = (((ans
                         + Ways(col + 1, comp, 0, 0))
                        + Ways(col + 1, comp, 1, 1))
                       + Ways(col + 1, comp, 0, 1));
                ans = (ans
                       + Ways(col + 1, comp + 2, 1, 0));
            }
        }
  
        // Memoization
        return dp[col][comp][row1][row2] = ans;
    }
}
  
// Driver Code
signed main()
{
    n = 2;
    k = 1;
    memset(dp, -1, sizeof(dp));
  
    // Initially at first coloumn
    // with 0 components
    cout << Ways(1, 0, 0, 0);
    return 0;
}


Java
// Java implementation to find the
// number of ways to make exactly
// C components in a 2 * N matrix
class GFG{
  
// row1 and row2 are one
// when both are same colored
static int n, k;
static int [][][][]dp = new int[1024][2048][2][2];
  
// Function to find the number of
// ways to make exactly C components
// in a 2 * N matrix
static int Ways(int col, int comp,
                int row1, int row2)
{
  
    // if No of components
    // at any stage exceeds
    // the given number
    // then base case
    if (comp > k)
        return 0;
  
    if (col > n)
    {
        if (comp == k)
            return 1;
        else
            return 0;
    }
  
    // Condition to check
    // if already visited
    if (dp[col][comp][row1][row2] != -1)
        return dp[col][comp][row1][row2];
  
    // if not visited previously
    else
    {
        int ans = 0;
  
        // At the first column
        if (col == 1) 
        {
  
            // color {white, white} or
            // {black, black}
            ans = (ans + Ways(col + 1, comp + 1, 0, 0) + 
                         Ways(col + 1, comp + 1, 1, 1));
  
            // Color {white, black} or
            // {black, white}
            ans = (ans + Ways(col + 1, comp + 2, 0, 1) + 
                         Ways(col + 1, comp + 2, 1, 0));
        }
        else 
        {
  
            // If previous both
            // rows have same color
            if ((row1 > 0 && row2 > 0) || 
                (row1 == 0 && row2 ==0)) 
            {
  
                // Fill with {same, same} and
                // {white, black} and {black, white}
                ans = (((ans + 
                         Ways(col + 1, comp + 1, 0, 0)) + 
                         Ways(col + 1, comp + 1, 1, 0)) + 
                         Ways(col + 1, comp + 1, 0, 1));
  
                // Fill with same without
                // increase in component
                // as it has been
                // counted previously
                ans = (ans + 
                       Ways(col + 1, comp, 1, 1));
            }
  
            // When previous rows
            // had {white, black}
            if (row1 > 0 && row2 == 0) 
            {
                ans = (((ans + 
                         Ways(col + 1, comp, 0, 0)) + 
                         Ways(col + 1, comp, 1, 1)) + 
                         Ways(col + 1, comp, 1, 0));
                ans = (ans + 
                       Ways(col + 1, comp + 2, 0, 1));
            }
  
            // When previous rows
            // had {black, white}
            if (row1 ==0 && row2 > 0)
            {
                ans = (((ans + 
                         Ways(col + 1, comp, 0, 0)) + 
                         Ways(col + 1, comp, 1, 1)) +
                         Ways(col + 1, comp, 0, 1));
                ans = (ans +
                       Ways(col + 1, comp + 2, 1, 0));
            }
        }
  
        // Memoization
        return dp[col][comp][row1][row2] = ans;
    }
}
  
// Driver Code
public static void main(String[] args)
{
    n = 2;
    k = 1;
    for (int i = 0; i < 1024; i++)
        for (int j = 0; j < 2048; j++)
            for (int k = 0; k < 2; k++)
                for (int l = 0; l < 2; l++)
                    dp[i][j][k][l] = -1;
  
    // Initially at first coloumn
    // with 0 components
    System.out.print(Ways(1, 0, 0, 0));
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# implementation to find the
// number of ways to make exactly
// C components in a 2 * N matrix
using System;
  
class GFG{
  
// row1 and row2 are one
// when both are same colored
static int n, k;
static int [,,,]dp = new int[ 1024, 2048, 2, 2 ];
  
// Function to find the number of
// ways to make exactly C components
// in a 2 * N matrix
static int Ways(int col, int comp,
                int row1, int row2)
{
      
    // If No of components
    // at any stage exceeds
    // the given number
    // then base case
    if (comp > k)
        return 0;
  
    if (col > n)
    {
        if (comp == k)
            return 1;
        else
            return 0;
    }
  
    // Condition to check
    // if already visited
    if (dp[ col, comp, row1, row2 ] != -1)
        return dp[ col, comp, row1, row2 ];
  
    // If not visited previously
    else
    {
        int ans = 0;
  
        // At the first column
        if (col == 1) 
        {
  
            // color {white, white} or
            // {black, black}
            ans = (ans + Ways(col + 1, comp + 1, 0, 0) + 
                         Ways(col + 1, comp + 1, 1, 1));
  
            // Color {white, black} or
            // {black, white}
            ans = (ans + Ways(col + 1, comp + 2, 0, 1) + 
                         Ways(col + 1, comp + 2, 1, 0));
        }
        else
        {
              
            // If previous both
            // rows have same color
            if ((row1 > 0 && row2 > 0) || 
                (row1 == 0 && row2 == 0)) 
            {
                  
                // Fill with {same, same} and
                // {white, black} and {black, white}
                ans = (((ans + 
                         Ways(col + 1, comp + 1, 0, 0)) + 
                         Ways(col + 1, comp + 1, 1, 0)) + 
                         Ways(col + 1, comp + 1, 0, 1));
  
                // Fill with same without
                // increase in component
                // as it has been
                // counted previously
                ans = (ans + 
                       Ways(col + 1, comp, 1, 1));
            }
  
            // When previous rows
            // had {white, black}
            if (row1 > 0 && row2 == 0) 
            {
                ans = (((ans + 
                         Ways(col + 1, comp, 0, 0)) + 
                         Ways(col + 1, comp, 1, 1)) + 
                         Ways(col + 1, comp, 1, 0));
                ans = (ans + 
                       Ways(col + 1, comp + 2, 0, 1));
            }
  
            // When previous rows
            // had {black, white}
            if (row1 == 0 && row2 > 0)
            {
                ans = (((ans + 
                         Ways(col + 1, comp, 0, 0)) + 
                         Ways(col + 1, comp, 1, 1)) +
                         Ways(col + 1, comp, 0, 1));
                ans = (ans +
                       Ways(col + 1, comp + 2, 1, 0));
            }
        }
  
        // Memoization
        return dp[ col, comp, row1, row2 ] = ans;
    }
}
  
// Driver Code 
public static void Main(String[] args)
{
    n = 2;
    k = 1;
      
    for(int i = 0; i < 1024; i++)
       for(int j = 0; j < 2048; j++)
          for(int K = 0; K < 2; K++)
             for(int l = 0; l < 2; l++)
                dp[ i, j, K, l ] = -1;
  
    // Initially at first coloumn
    // with 0 components
    Console.Write(Ways(1, 0, 0, 0));
}
}
  
// This code is contributed by Rajput-Ji


输出:
2

时间兼容性: O(N * C)