给定一个整数N ,任务是使用红色,黄色或绿色绘制一个大小为N x 3的网格,同时使相邻的像元对没有相同的 颜色。打印各种可能的不同方式
例子:
Input: N = 1
Output: 12
Explanation:
Following 12 possible ways to paint the grid exists:
- Red, Yellow, Red
- Yellow, Red, Yellow
- Green, Red, Yellow
- Red, Yellow, Green
- Yellow, Red, Green
- Green, Red, Green
- Red, Green, Red
- Yellow, Green, Red
- Green, Yellow, Red
- Red, Green, Yellow
- Yellow, Green, Yellow
- Green, Yellow, Green
Input: N = 2
Output: 102
方法:请按照以下步骤解决问题:
- 为行着色的方法可以分为以下两类:
- 最左边和最右边的单元格具有相同的颜色。
- 最左边和最右边的单元格具有不同的颜色。
- 考虑第一种情况:
- 存在六种可能的方式来绘制行,以使最左边和最右边的颜色相同。
- 对于同时占据最左边和最右边单元格的每种颜色,都有两种不同的颜色可用来对中间行进行着色。
- 考虑第二种情况:
- 存在六种可能的方式来绘制最左边和最右边的颜色是不同的。
- 左边的单元格有三个选择,中间的单元格有两个选择,并用唯一剩余的颜色填充最右边的单元格。因此,可能性的总数为3 * 2 * 1 = 6 。
- 现在,对于随后的单元格,请看以下示例:
- 如果将前一行绘制为红色绿色红色,则可以通过以下五种有效方法为当前行着色:
- {绿色红色绿色}
- {绿色红色黄色}
- {绿色黄绿色}
- {黄色红色绿色}
- {黄色红色黄色}
- 从以上观察可知,三种可能性的末端具有相同的颜色,而两种可能性的末端具有不同的颜色。
- 如果前一行的颜色为红色绿色黄色,则存在以下四种为当前行着色的可能性:
- {绿色红色绿色}
- {绿色黄色红色}
- {绿色黄绿色}
- {黄色红色绿色}
- 从上面的观察中可以明显看出,可能性的末端具有相同的颜色,而两种可能性的末端具有不同的颜色。
- 如果将前一行绘制为红色绿色红色,则可以通过以下五种有效方法为当前行着色:
- 因此,基于上述观察,可以为绘制N行的方式数量定义以下递归关系:
Count of ways to color current row having ends of same color SN+1 = 3 * SN + 2DN
Count of ways to color current row having ends of different colors DN+1 = 2 * SN + 2DN
Total number of ways to paint all N rows is equal to the sum of SN and DN.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number
// of ways to paint N * 3 grid
// based on given conditions
void waysToPaint(int n)
{
// Count of ways to pain a
// row with same colored ends
int same = 6;
// Count of ways to pain a row
// with different colored ends
int diff = 6;
// Traverse up to (N - 1)th row
for (int i = 0; i < n - 1; i++) {
// Calculate the count of ways
// to paint the current row
// For same colored ends
same = 3 * same + 2 * diff;
// For different colored ends
diff = 2 * same + 2 * diff;
}
// Print the total number of ways
cout << (same + diff);
}
// Driver Code
int main()
{
int N = 1;
waysToPaint(N);
}
// This code is contributed by ukasp.
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to count the number
// of ways to paint N * 3 grid
// based on given conditions
static void waysToPaint(int n)
{
// Count of ways to pain a
// row with same colored ends
long same = 6;
// Count of ways to pain a row
// with different colored ends
long diff = 6;
// Traverse up to (N - 1)th row
for (int i = 0; i < n - 1; i++) {
// Calculate the count of ways
// to paint the current row
// For same colored ends
same = 3 * same + 2 * diff;
// For different colored ends
diff = 2 * same + 2 * diff;
}
// Print the total number of ways
System.out.println(same + diff);
}
// Driver code
public static void main(String[] args)
{
int N = 1;
// Function call
waysToPaint(N);
}
}
Python3
# Python3 program for the above approach
# Function to count the number
# of ways to paint N * 3 grid
# based on given conditions
def waysToPaint(n):
# Count of ways to pain a
# row with same colored ends
same = 6
# Count of ways to pain a row
# with different colored ends
diff = 6
# Traverse up to (N - 1)th row
for _ in range(n - 1):
# Calculate the count of ways
# to paint the current row
# For same colored ends
same = 3 * same + 2 * diff
# For different colored ends
diff = 2 * same + 2 * diff
# Print the total number of ways
print(same + diff)
# Driver Code
N = 1
waysToPaint(N)
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number
// of ways to paint N * 3 grid
// based on given conditions
static void waysToPaint(int n)
{
// Count of ways to pain a
// row with same colored ends
int same = 6;
// Count of ways to pain a row
// with different colored ends
int diff = 6;
// Traverse up to (N - 1)th row
for(int i = 0; i < n - 1; i++)
{
// Calculate the count of ways
// to paint the current row
// For same colored ends
same = 3 * same + 2 * diff;
// For different colored ends
diff = 2 * same + 2 * diff;
}
// Print the total number of ways
Console.WriteLine(same + diff);
}
// Driver code
static public void Main()
{
int N = 1;
waysToPaint(N);
}
}
// This code is contributed by offbeat
Javascript
输出:
12
时间复杂度: O(N)
辅助空间: O(1)