-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1931_colorTheGrid.cc
115 lines (106 loc) · 2.17 KB
/
Problem_1931_colorTheGrid.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <cmath>
using namespace std;
// TODO: figure it out.
class Solution
{
private:
constexpr static int MAXN = 1001;
constexpr static int MAXM = 5;
constexpr static int MAXS = 3 * 3 * 3 * 3 * 3; // std::pow(3, MAXM)
constexpr static int MOD = 1e9 + 7;
int N;
int M;
int maxs;
int dp[MAXN][MAXM][MAXS];
int first[MAXS];
int size;
public:
int colorTheGrid(int m, int n)
{
build(m, n);
int ans = 0;
for (int i = 0; i < size; i++)
{
ans = (ans + f(1, 0, first[i], 1)) % MOD;
}
return ans;
}
void build(int rows, int cols)
{
N = std::max(rows, cols);
M = std::min(rows, cols);
maxs = (int) std::pow(3, M);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
for (int s = 0; s < maxs; s++)
{
dp[i][j][s] = -1;
}
}
}
size = 0;
dfs(0, 0, 1);
}
// 取得所有第一行的有效状态
void dfs(int j, int s, int bit)
{
if (j == M)
{
first[size++] = s;
}
else
{
int left = j == 0 ? -1 : get(s, bit / 3);
if (left != 0)
{
dfs(j + 1, set(s, bit, 0), bit * 3);
}
if (left != 1)
{
dfs(j + 1, set(s, bit, 1), bit * 3);
}
if (left != 2)
{
dfs(j + 1, set(s, bit, 2), bit * 3);
}
}
}
int f(int i, int j, int s, int bit)
{
if (i == N)
{
return 1;
}
if (j == M)
{
return f(i + 1, 0, s, 1);
}
if (dp[i][j][s] != -1)
{
return dp[i][j][s];
}
// 上方的颜色
int up = get(s, bit);
// 左侧的颜色,-1代表左侧没有格子
int left = j == 0 ? -1 : get(s, bit / 3);
int ans = 0;
if (up != 0 && left != 0)
{
ans = (ans + f(i, j + 1, set(s, bit, 0), bit * 3)) % MOD;
}
if (up != 1 && left != 1)
{
ans = (ans + f(i, j + 1, set(s, bit, 1), bit * 3)) % MOD;
}
if (up != 2 && left != 2)
{
ans = (ans + f(i, j + 1, set(s, bit, 2), bit * 3)) % MOD;
}
dp[i][j][s] = ans;
return ans;
}
int get(int s, int bit) { return s / bit % 3; }
int set(int s, int bit, int v) { return s - get(s, bit) * bit + v * bit; }
};