Skip to content

Commit 1ddbb16

Browse files
authored
update: 添加问题“1931.用三种不同颜色为网格涂色”的代码和题解(#944)
* feat: 脚本生成newSolutions.py时带上题目链接 不过这个用途不是很大,一般使用脚本生成的时候都是当天,而补题时最需要题目链接。 (#941) Signed-off-by: LetMeFly666 <[email protected]> * MGJW: rename (#941) Signed-off-by: LetMeFly666 <[email protected]> * 1931: half.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 1931: half.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 1931: half.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 1931: half.cpp (#941) 每次" half\都是想重新设计数据结构了 Signed-off-by: LetMeFly666 <[email protected]> * 1931: WA.cpp (#941) - all0 Signed-off-by: LetMeFly666 <[email protected]> * 1931: AC.cpp (#941) - AC,20.09%,79.91% 竟然这就过了!!! 聊天状态下接近一遍过 但肯定还有更优的方法 Signed-off-by: LetMeFly666 <[email protected]> * 3337: 添加矩阵快速幂的标签 (#941) 我说咋找不到 Signed-off-by: LetMeFly666 <[email protected]> * docs: 矩阵快速幂(1931) (#941) Archive - 虽然还很想继续写,但是准备先睡觉了 Signed-off-by: LetMeFly666 <[email protected]> * 3337: CE.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 3337: fix.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 3337: RE.cpp (#941) Signed-off-by: LetMeFly666 <[email protected]> * 3337: WA.cpp (#941) - 0/84 Signed-off-by: LetMeFly666 <[email protected]> * 3337: AC.cpp (#941) - AC,矩阵快速幂,5.19%,7.74% Signed-off-by: LetMeFly666 <[email protected]> * docs: 1931 - rename (#941) Signed-off-by: LetMeFly666 <[email protected]> * docs: 1931 - rename (#941) Signed-off-by: LetMeFly666 <[email protected]> * docs: 1931 - template (#941) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“1931.用三种不同颜色为网格涂色”的代码和题解(#944) feat: 脚本生成newSolutions.py时带上题目链接 不过这个用途不是很大,一般使用脚本生成的时候都是当天,而补题时最需要题目链接。 Signed-off-by: LetMeFly666 <[email protected]> * clean: .commitmsg (#941) Signed-off-by: LetMeFly666 <[email protected]> * clean: codes (#941) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent b136d5a commit 1ddbb16

13 files changed

+513
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-19 21:52:57
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-19 23:11:19
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
static constexpr int pow[6] = {1, 3, 9, 27, 81, 243};
14+
static constexpr int MOD = 1e9 + 7;
15+
16+
inline int getTh(int a, int n) {
17+
return a / pow[n] % 3;
18+
}
19+
20+
bool ok(int a, int b, int m) {
21+
if (a % 3 == b % 3) {
22+
return false;
23+
}
24+
for (int i = 1; i < m; i++) {
25+
if (getTh(a, i) == getTh(b, i) || getTh(a, i) == getTh(a, i - 1) || getTh(b, i) == getTh(b, i - 1)) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
public:
32+
int colorTheGrid(int m, int n) {
33+
int types = pow[m];
34+
unordered_map<int, vector<int>> okList;
35+
for (int i = 0; i < types; i++) {
36+
for (int j = 0; j < types; j++) {
37+
if (ok(i, j, m)) {
38+
okList[i].push_back(j);
39+
}
40+
}
41+
}
42+
int ok1type = okList.size();
43+
vector<int> firstCol(ok1type, 1), secondCol(ok1type);
44+
unordered_map<int, int> code2idx(ok1type);
45+
unordered_map<int, int> idx2code(ok1type);
46+
int th = 0;
47+
for (unordered_map<int, vector<int>>::iterator it = okList.begin(); it != okList.end(); it++, th++) {
48+
code2idx[it->first] = th;
49+
idx2code[th] = it->first;
50+
}
51+
for (int j = 2; j <= n; j++) {
52+
for (int t = 0; t < ok1type; t++) {
53+
int lastCode = idx2code[t];
54+
for (int nextCode : okList[lastCode]) {
55+
secondCol[code2idx[nextCode]] = (secondCol[code2idx[nextCode]] + firstCol[t]) % MOD;
56+
}
57+
}
58+
firstCol = move(secondCol);
59+
secondCol = vector<int>(ok1type);
60+
}
61+
int ans = 0;
62+
for (int t : firstCol) {
63+
ans = (ans + t) % MOD;
64+
}
65+
return ans;
66+
}
67+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-20 08:59:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-20 09:34:32
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
typedef vector<vector<int>> matrix;
13+
14+
class Solution {
15+
private:
16+
int singleNum;
17+
static constexpr int MOD = 1e9 + 7;
18+
static constexpr int pow[6] = {1, 3, 9, 27, 81, 243};
19+
20+
inline int getTh(int a, int n) {
21+
return a / pow[n] % 3;
22+
}
23+
24+
// 单看一列是否可行
25+
inline bool isOk(int a, int m) {
26+
for (int i = 1; i < m; i++) {
27+
if (getTh(a, i) == getTh(a, i - 1)) {
28+
return false;
29+
}
30+
}
31+
return true;
32+
}
33+
34+
// 看可行的两列是否能相邻
35+
inline bool isOk(int a, int b, int m) {
36+
for (int i = 0; i < m; i++) {
37+
if (getTh(a, i) == getTh(b, i)) {
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
44+
matrix mul(matrix a, matrix b) {
45+
matrix ans(singleNum, vector<int>(singleNum));
46+
for (int i = 0; i < singleNum; i++) {
47+
for (int k = 0; k < singleNum; k++) {
48+
for (int j = 0; j < singleNum; j++) {
49+
ans[i][j] = (ans[i][j] + (ll)a[i][k] * b[k][j] % MOD) % MOD;
50+
}
51+
}
52+
}
53+
return ans;
54+
}
55+
56+
matrix Pow(matrix a, int b) {
57+
matrix ans(singleNum, vector<int>(singleNum));
58+
for (int i = 0; i < singleNum; i++) {
59+
ans[i][i] = 1;
60+
}
61+
while (b) {
62+
if (b & 1) {
63+
ans = mul(ans, a);
64+
}
65+
a = mul(a, a);
66+
b >>= 1;
67+
}
68+
return ans;
69+
}
70+
public:
71+
int colorTheGrid(int m, int n) {
72+
vector<int> singleOk;
73+
for (int i = 0; i < pow[m]; i++) {
74+
if (isOk(i, m)) {
75+
singleOk.push_back(i);
76+
}
77+
}
78+
singleNum = singleOk.size(); // 即使m=5也一共有48种
79+
// printf("%d\n", singleNum);
80+
matrix M(singleNum, vector<int>(singleNum));
81+
for (int i = 0; i < singleNum; i++) {
82+
for (int j = 0; j < singleNum; j++) {
83+
if (isOk(singleOk[i], singleOk[j], m)) {
84+
M[j][i] = 1;
85+
}
86+
}
87+
}
88+
matrix Mn = Pow(M, n - 1);
89+
int ans = 0;
90+
for (int i = 0; i < singleNum; i++) {
91+
for (int j = 0; j < singleNum; j++) {
92+
ans = (ans + Mn[i][j]) % MOD;
93+
}
94+
}
95+
return ans;
96+
}
97+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@
637637
|1920.基于排列构建数组|简单|<a href="https://leetcode.cn/problems/build-array-from-permutation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/06/LeetCode%201920.%E5%9F%BA%E4%BA%8E%E6%8E%92%E5%88%97%E6%9E%84%E5%BB%BA%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147748208" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/build-array-from-permutation/solutions/3670248/letmefly-1920ji-yu-pai-lie-gou-jian-shu-p0pif/" target="_blank">LeetCode题解</a>|
638638
|1922.统计好数字的数目|中等|<a href="https://leetcode.cn/problems/count-good-numbers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/14/LeetCode%201922.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E6%95%B0%E5%AD%97%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147200001" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-numbers/solutions/3650327/letmefly-1922tong-ji-hao-shu-zi-de-shu-m-ev7k/" target="_blank">LeetCode题解</a>|
639639
|1928.规定时间内到达终点的最小花费|困难|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/03/LeetCode%201928.%E8%A7%84%E5%AE%9A%E6%97%B6%E9%97%B4%E5%86%85%E5%88%B0%E8%BE%BE%E7%BB%88%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142691241" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/solutions/2937779/letmefly-1928gui-ding-shi-jian-nei-dao-d-h1rk/" target="_blank">LeetCode题解</a>|
640+
|1931.用三种不同颜色为网格涂色|困难|<a href="https://leetcode.cn/problems/painting-a-grid-with-three-different-colors/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/20/LeetCode%201931.%E7%94%A8%E4%B8%89%E7%A7%8D%E4%B8%8D%E5%90%8C%E9%A2%9C%E8%89%B2%E4%B8%BA%E7%BD%91%E6%A0%BC%E6%B6%82%E8%89%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148082436" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/painting-a-grid-with-three-different-colors/solutions/3681550/letmefly-1931yong-san-chong-bu-tong-yan-yenqo/" target="_blank">LeetCode题解</a>|
640641
|1944.队列中可以看到的人数|困难|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/05/LeetCode%201944.%E9%98%9F%E5%88%97%E4%B8%AD%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E7%9A%84%E4%BA%BA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135416441" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/solutions/2592403/letmefly-1944dui-lie-zhong-ke-yi-kan-dao-e8p6/" target="_blank">LeetCode题解</a>|
641642
|1945.字符串转化后的各位数字之和|简单|<a href="https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/15/LeetCode%201945.%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E5%8C%96%E5%90%8E%E7%9A%84%E5%90%84%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128335606" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/solutions/2021840/letmefly-1945zi-fu-chuan-zhuan-hua-hou-d-569r/" target="_blank">LeetCode题解</a>|
642643
|1953.你可以工作的最大周数|中等|<a href="https://leetcode.cn/problems/maximum-number-of-weeks-for-which-you-can-work/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/16/LeetCode%201953.%E4%BD%A0%E5%8F%AF%E4%BB%A5%E5%B7%A5%E4%BD%9C%E7%9A%84%E6%9C%80%E5%A4%A7%E5%91%A8%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138974368" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-number-of-weeks-for-which-you-can-work/solutions/2780018/letmefly-1953ni-ke-yi-gong-zuo-de-zui-da-1h8h/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)