Skip to content

Commit b24bdca

Browse files
authored
update: 添加问题“840.矩阵中的幻方”的代码和题解 (#1293)
* word: en (#1292) * 849: WA.cpp (#1292) * 840: AC.cpp+WA.py (#1292) cpp - AC,100.00%,69.12% * 840: AC.py (#1292) - AC,23.81%,100.00% * 840: half.题解 (#1292) - 憩 * update: 添加问题“840.矩阵中的幻方”的代码和题解 (#1293) Signed-off-by: LetMeFly666 <814114971@qq.com> --------- Signed-off-by: LetMeFly666 <814114971@qq.com>
1 parent 947256e commit b24bdca

File tree

5 files changed

+272
-0
lines changed

5 files changed

+272
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-30 13:14:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-30 13:25:00
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
inline bool is(vector<vector<int>>& grid, int i, int j) {
14+
int mask = 0;
15+
int rowCnt[3] = {0}, colCnt[3] = {0};
16+
for (int di = 0; di < 3; di++) {
17+
for (int dj = 0; dj < 3; dj++) {
18+
int v = grid[i - di][j - dj];
19+
mask |= 1 << v;
20+
rowCnt[di] += v;
21+
colCnt[dj] += v;
22+
}
23+
}
24+
if (mask != (1 << 10) - 2) { // (1<<10)-1:1111111111(10个1)而mask没有或上1<<0所以再-1
25+
return false;
26+
}
27+
28+
int cnt = rowCnt[0];
29+
for (int d = 0; d < 3; d++) {
30+
if (rowCnt[d] != cnt || colCnt[d] != cnt) {
31+
return false;
32+
}
33+
}
34+
if (grid[i][j] + grid[i - 1][j - 1] + grid[i - 2][j - 2] != cnt) {
35+
return false;
36+
}
37+
if (grid[i - 2][j] + grid[i - 1][j - 1] + grid[i][j - 2] != cnt) {
38+
return false;
39+
}
40+
return true;
41+
}
42+
public:
43+
int numMagicSquaresInside(vector<vector<int>>& grid) {
44+
int ans = 0;
45+
int n = grid.size(), m = grid[0].size();
46+
for (int i = 2; i < n; i++) {
47+
for (int j = 2; j < m; j++) {
48+
ans += is(grid, i, j);
49+
}
50+
}
51+
return ans;
52+
}
53+
};
54+
55+
#if defined(_WIN32) || defined(__APPLE__)
56+
/*
57+
[[4,3,8,4],[9,5,1,9],[2,7,6,2]]
58+
59+
1
60+
*/
61+
void dbg1to9() {
62+
int mask = 0;
63+
for (int i = 1; i < 10; i++) {
64+
mask |= 1 << i;
65+
}
66+
cout << mask << endl; // 1022不是1023,因为mask没有或上1<<0
67+
}
68+
69+
int main() {
70+
dbg1to9();
71+
72+
string s;
73+
while (cin >> s) {
74+
Solution sol;
75+
vector<vector<int>> v = stringToVectorVector(s);
76+
cout << sol.numMagicSquaresInside(v) << endl;
77+
}
78+
return 0;
79+
}
80+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-12-30 13:14:11
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-12-30 13:38:58
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def ok(self, grid: List[List[int]], i: int, j: int) -> bool:
11+
mask = 0
12+
colCnt = [0] * 3
13+
rowCnt = [0] * 3
14+
for di in range(3):
15+
for dj in range(3):
16+
v = grid[i - di][j - dj]
17+
mask |= 1 << v
18+
rowCnt[di] += v
19+
colCnt[dj] += v
20+
if mask != (1 << 10) - 2:
21+
return False
22+
cnt = grid[i][j] + grid[i - 1][j - 1] + grid[i - 2][j - 2]
23+
if grid[i - 2][j] + grid[i - 1][j - 1] + grid[i][j - 2] != cnt:
24+
return False
25+
if any(c != cnt for c in colCnt):
26+
return False
27+
if any(r != cnt for r in rowCnt):
28+
return False
29+
return True
30+
31+
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
32+
return sum(self.ok(grid, i, j) for j in range(2, len(grid[0])) for i in range(2, len(grid)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@
432432
|0831.隐藏个人信息|中等|<a href="https://leetcode.cn/problems/masking-personal-information/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/04/01/LeetCode%200831.%E9%9A%90%E8%97%8F%E4%B8%AA%E4%BA%BA%E4%BF%A1%E6%81%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129893329" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/masking-personal-information/solutions/2202064/letmefly-831yin-cang-ge-ren-xin-xi-by-ti-xkxe/" target="_blank">LeetCode题解</a>|
433433
|0833.字符串中的查找与替换|中等|<a href="https://leetcode.cn/problems/find-and-replace-in-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/15/LeetCode%200833.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%9B%BF%E6%8D%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132289306" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-and-replace-in-string/solutions/2388863/letmefly-833zi-fu-chuan-zhong-de-cha-zha-f9vs/" target="_blank">LeetCode题解</a>|
434434
|0837.新21点|中等|<a href="https://leetcode.cn/problems/new-21-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/17/LeetCode%200837.%E6%96%B021%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/150474266" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/new-21-game/solutions/3755850/letmefly-837xin-21-dian-dong-tai-gui-hua-8o4t/" target="_blank">LeetCode题解</a>|
435+
|0840.矩阵中的幻方|中等|<a href="https://leetcode.cn/problems/magic-squares-in-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/30/LeetCode%200840.%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E5%B9%BB%E6%96%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156429151" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/magic-squares-in-grid/solutions/3869464/letmefly-840ju-zhen-zhong-de-huan-fang-m-m5ev/" target="_blank">LeetCode题解</a>|
435436
|0849.到最近的人的最大距离|中等|<a href="https://leetcode.cn/problems/maximize-distance-to-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/22/LeetCode%200849.%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA%E7%9A%84%E6%9C%80%E5%A4%A7%E8%B7%9D%E7%A6%BB/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132420545" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximize-distance-to-closest-person/solutions/2399151/letmefly-849dao-zui-jin-de-ren-de-zui-da-dnw3/" target="_blank">LeetCode题解</a>|
436437
|0856.括号的分数|中等|<a href="https://leetcode.cn/problems/score-of-parentheses/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/09/LeetCode%200856.%E6%8B%AC%E5%8F%B7%E7%9A%84%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127221656" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/score-of-parentheses/solution/letmefly-856gua-hao-de-fen-shu-by-tisfy-v01a/" target="_blank">LeetCode题解</a>|
437438
|0860.柠檬水找零|简单|<a href="https://leetcode.cn/problems/lemonade-change/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/22/LeetCode%200860.%E6%9F%A0%E6%AA%AC%E6%B0%B4%E6%89%BE%E9%9B%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131864033" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lemonade-change/solutions/2353759/letmefly-860ning-meng-shui-zhao-ling-fu-5v6m1/" target="_blank">LeetCode题解</a>|
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: 840.矩阵中的幻方:模拟(+小小位运算)
3+
date: 2025-12-30 13:40:02
4+
tags: [题解, LeetCode, 中等, 数组, 哈希表, 数学, 矩阵, 模拟, 位运算]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】840.矩阵中的幻方:模拟(+小小位运算)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/magic-squares-in-grid/](https://leetcode.cn/problems/magic-squares-in-grid/)
11+
12+
<p><code>3 x 3</code> 的幻方是一个填充有&nbsp;<strong>从 <code>1</code> 到 <code>9</code>&nbsp;</strong> 的不同数字的 <code>3 x 3</code> 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。</p>
13+
14+
<p>给定一个由整数组成的<code>row x col</code>&nbsp;的 <code>grid</code>,其中有多少个&nbsp;<code>3 × 3</code> 的 “幻方” 子矩阵?</p>
15+
16+
<p>注意:虽然幻方只能包含 1 到 9 的数字,但&nbsp;<code>grid</code> 可以包含最多15的数字。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<p><img src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" /></p>
23+
24+
<pre>
25+
<strong>输入: </strong>grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]
26+
<strong>输出: </strong>1
27+
<strong>解释: </strong>
28+
下面的子矩阵是一个 3 x 3 的幻方:
29+
<img src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" />
30+
而这一个不是:
31+
<img src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" />
32+
总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong> grid = [[8]]
39+
<strong>输出:</strong> 0
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>row == grid.length</code></li>
48+
<li><code>col == grid[i].length</code></li>
49+
<li><code>1 &lt;= row, col &lt;= 10</code></li>
50+
<li><code>0 &lt;= grid[i][j] &lt;= 15</code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法:模拟
56+
57+
主函数中枚举每个`3x3`矩阵的右下角下表,写一个辅助函数计算右下角坐标为`(i, j)`的矩阵是否是`幻方`
58+
59+
这个函数怎么写呢?
60+
61+
对于是否由1-9组成,可以使用位运算,将所有数与初始值为0的mask按位或,如出现3则$mask |= 1 << 3$,最终看$mask$是否为$1<<10-2$。
62+
63+
我们还可以使用两个大小为3的数组分别记录每一行和每一列的和,看他们是否相等、以及是否和主对角线和副对角线的和相等。
64+
65+
+ 时间复杂度$O(col\times row)$
66+
+ 空间复杂度$O(1)$
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
/*
74+
* @LastEditTime: 2025-12-30 13:25:00
75+
*/
76+
class Solution {
77+
private:
78+
inline bool is(vector<vector<int>>& grid, int i, int j) {
79+
int mask = 0;
80+
int rowCnt[3] = {0}, colCnt[3] = {0};
81+
for (int di = 0; di < 3; di++) {
82+
for (int dj = 0; dj < 3; dj++) {
83+
int v = grid[i - di][j - dj];
84+
mask |= 1 << v;
85+
rowCnt[di] += v;
86+
colCnt[dj] += v;
87+
}
88+
}
89+
if (mask != (1 << 10) - 2) { // (1<<10)-1:1111111111(10个1)而mask没有或上1<<0所以再-1
90+
return false;
91+
}
92+
93+
int cnt = rowCnt[0];
94+
for (int d = 0; d < 3; d++) {
95+
if (rowCnt[d] != cnt || colCnt[d] != cnt) {
96+
return false;
97+
}
98+
}
99+
if (grid[i][j] + grid[i - 1][j - 1] + grid[i - 2][j - 2] != cnt) {
100+
return false;
101+
}
102+
if (grid[i - 2][j] + grid[i - 1][j - 1] + grid[i][j - 2] != cnt) {
103+
return false;
104+
}
105+
return true;
106+
}
107+
public:
108+
int numMagicSquaresInside(vector<vector<int>>& grid) {
109+
int ans = 0;
110+
int n = grid.size(), m = grid[0].size();
111+
for (int i = 2; i < n; i++) {
112+
for (int j = 2; j < m; j++) {
113+
ans += is(grid, i, j);
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
#### Python
122+
123+
```python
124+
'''
125+
LastEditTime: 2025-12-30 13:38:58
126+
'''
127+
from typing import List
128+
129+
class Solution:
130+
def ok(self, grid: List[List[int]], i: int, j: int) -> bool:
131+
mask = 0
132+
colCnt = [0] * 3
133+
rowCnt = [0] * 3
134+
for di in range(3):
135+
for dj in range(3):
136+
v = grid[i - di][j - dj]
137+
mask |= 1 << v
138+
rowCnt[di] += v
139+
colCnt[dj] += v
140+
if mask != (1 << 10) - 2:
141+
return False
142+
cnt = grid[i][j] + grid[i - 1][j - 1] + grid[i - 2][j - 2]
143+
if grid[i - 2][j] + grid[i - 1][j - 1] + grid[i][j - 2] != cnt:
144+
return False
145+
if any(c != cnt for c in colCnt):
146+
return False
147+
if any(r != cnt for r in rowCnt):
148+
return False
149+
return True
150+
151+
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
152+
return sum(self.ok(grid, i, j) for j in range(2, len(grid[0])) for i in range(2, len(grid)))
153+
```
154+
155+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156429151)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/30/LeetCode%200840.%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E5%B9%BB%E6%96%B9/)~
156+
>
157+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,8 @@ categories: [自用]
17061706
|||
17071707
|menace|n. 威胁,危险的人/物,令人恐怖的氛围<br/>v. 对...构成危险,危及|
17081708
|engraving|n. 版画,雕版印刷品,雕刻(术)|
1709+
|||
1710+
|ecstasy|n. 狂喜,陶醉,入迷;迷幻药|
17091711

17101712
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
17111713
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)