Skip to content

Commit 4e25245

Browse files
authored
Create 1931.Painting-a-Grid-With-Three-Different-Colors.cpp
1 parent b9d0a09 commit 4e25245

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
typedef long long LL;
2+
class Solution {
3+
LL M = 1e9+7;
4+
public:
5+
int colorTheGrid(int m, int n)
6+
{
7+
vector<int>cand;
8+
for (int state = 0; state < pow(3,m); state++)
9+
{
10+
vector<int>temp;
11+
int state0 = state;
12+
int flag = 1;
13+
for (int i=0; i<m; i++)
14+
{
15+
int color = state0 % 3;
16+
if (!temp.empty() && temp.back()==color)
17+
{
18+
flag = 0;
19+
break;
20+
}
21+
temp.push_back(color);
22+
state0 /= 3;
23+
}
24+
if (flag==1)
25+
cand.push_back(state);
26+
}
27+
28+
int k = cand.size();
29+
vector<LL>dp(k, 1);
30+
31+
for (int j=1; j<n; j++)
32+
{
33+
vector<LL>dp2(k);
34+
for (int s=0; s<k; s++)
35+
for (int t = 0; t<k; t++)
36+
{
37+
if (checkOK(cand[s],cand[t],m))
38+
dp2[s] = (dp2[s] + dp[t]) % M;
39+
}
40+
dp = std::move(dp2);
41+
}
42+
43+
LL ret = 0;
44+
for (int s=0; s<k; s++)
45+
ret = (ret + dp[s])%M;
46+
return ret;
47+
48+
}
49+
50+
bool checkOK(int s, int t, int m)
51+
{
52+
for (int i=0; i<m; i++)
53+
{
54+
if (s%3==t%3)
55+
return false;
56+
s /=3;
57+
t /=3;
58+
}
59+
return true;
60+
}
61+
};

0 commit comments

Comments
 (0)