给定一个整数N ,任务是使用Red 、 Yellow或Green颜色绘制大小为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 。
- 现在,对于后续单元格,请查看以下示例:
- 如果前一行被绘制为Red Green Red ,则有以下五种有效方法可以为当前行着色:
- {绿红绿}
- {绿红黄}
- {绿黄绿}
- {黄红绿}
- {黄红黄}
- 从上面的观察可以看出,三种可能性的结尾颜色相同,两种可能性的结尾颜色不同。
- 如果前一行被着色为Red Green Yellow ,则存在以下四种着色当前行的可能性:
- {绿红绿}
- {绿黄红}
- {绿黄绿}
- {黄红绿}
- 从上面的观察,很明显,可能性有相同颜色的结束,两种可能性有不同颜色的结束。
- 如果前一行被绘制为Red Green Red ,则有以下五种有效方法可以为当前行着色:
- 因此,基于上述观察,可以为绘制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
long sameTmp = 3 * same + 2 * diff;
// For different colored ends
long diffTmp = 2 * same + 2 * diff;
same = sameTmp;
diff = diffTmp;
}
// Print the total number of ways
cout << (same + diff);
}
// Driver Code
int main()
{
int N = 2;
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
long sameTmp = 3 * same + 2 * diff;
// For different colored ends
long diffTmp = 2 * same + 2 * diff;
same = sameTmp;
diff = diffTmp;
}
// Print the total number of ways
System.out.println(same + diff);
}
// Driver code
public static void main(String[] args)
{
int N = 2;
// 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
sameTmp = 3 * same + 2 * diff
# For different colored ends
diffTmp = 2 * same + 2 * diff
same = sameTmp
diff = diffTmp
# Print the total number of ways
print(same + diff)
# Driver Code
N = 2
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
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
long sameTmp = 3 * same + 2 * diff;
// For different colored ends
long diffTmp = 2 * same + 2 * diff;
same = sameTmp;
diff = diffTmp;
}
// Print the total number of ways
Console.WriteLine(same + diff);
}
// Driver code
static public void Main()
{
int N = 2;
waysToPaint(N);
}
}
// This code is contributed by offbeat
Javascript
输出:
12
时间复杂度: O(N)
辅助空间: O(1)