📜  M * N表的每个块的颜色边界着色方法数量

📅  最后修改于: 2021-04-24 20:19:56             🧑  作者: Mango

给定一个表M * N。总共有M * N个大小为1的正方形。您必须用橙色,蓝色或黑色3种颜色为所有正方形的每一边上色,以使每个正方形具有2种不同的颜色,并且每种颜色必须出现两次。
这意味着每个正方形都有四个边,其中2个是橙色,其中2个是蓝色,其中2个是橙色,其中2个是黑色,其中2个是蓝色,其中2个是黑色。
例子:

方法:

  • 查找填充矩形的上部和右侧的方法的数量。上侧有M个单位,右侧有N个单位。因此,为矩形的上,右侧上色的方式是pow(3,M + N),因为每种方法都有3个选项。
  • 现在,为填充每个正方形的下部和左侧,每个正方形有2个选项,则行数为pow(2,M * N)
  • 最终结果是:
    Total count = pow(3, M + N ) *  pow(2, M * N)
    

下面是上述方法的实现:

C++
// C++ program to count the number 
// of ways to color boundary of 
// each block of M*N table.
#include 
using namespace std;
  
// Function to compute all way to 
// fill the boundary of all sides
// of the unit square
int CountWays(int N, int M)
{
    int count = 1;
  
    // Count possible ways to fill
    // all upper and left side of 
    // the rectangle M*N
    count = pow(3, M + N);
  
    // Count possible ways to fill
    // all  side of the all squares 
    // unit size
    count *= pow(2, M * N);
  
    return count;
}
  
// Driver code
int main()
{
  
    // Number of rows
    int N = 3; 
      
    // Number of columns
    int M = 2; 
      
    cout << CountWays(N, M);
    return 0;
}


Java
// Java program to count the number 
// of ways to color boundary of 
// each block of M*N table.
import java.util.*; 
  
class GFG{ 
  
// Function to compute all way to 
// fill the boundary of all sides
// of the unit square
static int CountWays(int N, int M)
{
    int count = 1;
  
    // Count possible ways to fill
    // all upper and left side of 
    // the rectangle M*N
    count = (int)Math.pow(3, M + N);
  
    // Count possible ways to fill
    // all side of the all squares 
    // unit size
    count *= (int)Math.pow(2, M * N);
  
    return count;
}
  
// Driver code
public static void main(String args[])
{
  
    // Number of rows
    int N = 3; 
      
    // Number of columns
    int M = 2; 
      
    System.out.println(CountWays(N, M));
}
}
  
// This code is contributed by ANKITKUMAR34


Python3
# Python3 program to count the number 
# of ways to color boundary of 
# each block of M*N table.
  
# Function to compute all way to 
# fill the boundary of all sides
# of the unit square
def CountWays(N, M):
      
    count = 1
  
    # Count possible ways to fill
    # all upper and left side of 
    # the rectangle M*N
    count = pow(3, M + N)
  
    # Count possible ways to fill
    # all side of the all squares 
    # unit size
    count *= pow(2, M * N);
  
    return count
  
# Driver code
  
# Number of rows
N = 3
      
# Number of columns
M = 2
      
print(CountWays(N, M))
  
# This code is contributed by ANKITKUMAR34


C#
// C# program to count the number 
// of ways to color boundary of 
// each block of M*N table. 
using System;
  
class GFG{
      
// Function to compute all way to 
// fill the boundary of all sides 
// of the unit square 
static int CountWays(int N, int M) 
{ 
    int count = 1; 
  
    // Count possible ways to fill 
    // all upper and left side of 
    // the rectangle M*N 
    count = (int)Math.Pow(3, M + N); 
  
    // Count possible ways to fill 
    // all side of the all squares 
    // unit size 
    count *= (int)Math.Pow(2, M * N); 
  
    return count; 
} 
  
// Driver code
static void Main()
{
      
    // Number of rows 
    int N = 3; 
      
    // Number of columns 
    int M = 2; 
  
    Console.Write(CountWays(N, M));
}
}
  
// This code is contributed by divyeshrabadiya07


输出:
15552