LeetCode 题解工作台
检查操作是否合法
给你一个下标从 0 开始的 8 x 8 网格 board ,其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 '.' 表示,白色格子用 'W' 表示,黑色格子用 'B' 表示。 游戏中每次操作步骤为:选择一个空格子,将它变成你正在执行的颜色(要么白色,要么黑色)。但是…
3
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·matrix
答案摘要
我们枚举所有可能的方向,对于每个方向 $(a, b)$,我们从 $(\textit{rMove}, \textit{cMove})$ 出发,用一个变量 记录我们走过的格子数,如果我们在走的过程中遇到了颜色为 的格子,且 $\textit{cnt} > 1$,那么我们就找到了一个好线段,返回 。 枚举结束后,如果我们没有找到任何好线段,那么返回 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·matrix 题型思路
题目描述
给你一个下标从 0 开始的 8 x 8 网格 board ,其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 '.' 表示,白色格子用 'W' 表示,黑色格子用 'B' 表示。
游戏中每次操作步骤为:选择一个空格子,将它变成你正在执行的颜色(要么白色,要么黑色)。但是,合法 操作必须满足:涂色后这个格子是 好线段的一个端点 (好线段可以是水平的,竖直的或者是对角线)。
好线段 指的是一个包含 三个或者更多格子(包含端点格子)的线段,线段两个端点格子为 同一种颜色 ,且中间剩余格子的颜色都为 另一种颜色 (线段上不能有任何空格子)。你可以在下图找到好线段的例子:
给你两个整数 rMove 和 cMove 以及一个字符 color ,表示你正在执行操作的颜色(白或者黑),如果将格子 (rMove, cMove) 变成颜色 color 后,是一个 合法 操作,那么返回 true ,如果不是合法操作返回 false 。
示例 1:

输入:board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B" 输出:true 解释:'.','W' 和 'B' 分别用颜色蓝色,白色和黑色表示。格子 (rMove, cMove) 用 'X' 标记。 以选中格子为端点的两个好线段在上图中用红色矩形标注出来了。
示例 2:

输入:board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W" 输出:false 解释:虽然选中格子涂色后,棋盘上产生了好线段,但选中格子是作为中间格子,没有产生以选中格子为端点的好线段。
提示:
board.length == board[r].length == 80 <= rMove, cMove < 8board[rMove][cMove] == '.'color要么是'B'要么是'W'。
解题思路
方法一:枚举
我们枚举所有可能的方向,对于每个方向 ,我们从 出发,用一个变量 记录我们走过的格子数,如果我们在走的过程中遇到了颜色为 的格子,且 ,那么我们就找到了一个好线段,返回 。
枚举结束后,如果我们没有找到任何好线段,那么返回 。
时间复杂度 ,其中 为 的行数,而 为 的列数,本题中 。空间复杂度 。
class Solution:
def checkMove(
self, board: List[List[str]], rMove: int, cMove: int, color: str
) -> bool:
for a in range(-1, 2):
for b in range(-1, 2):
if a == 0 and b == 0:
continue
i, j = rMove, cMove
cnt = 0
while 0 <= i + a < 8 and 0 <= j + b < 8:
cnt += 1
i, j = i + a, j + b
if cnt > 1 and board[i][j] == color:
return True
if board[i][j] in (color, "."):
break
return False
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(1) since the board size is fixed at 8x8 and each direction has at most 7 cells to check. Space complexity is O(1) as only temporary variables are used for direction checks without extra structures. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Asks if all eight directions are considered for a good line
- question_mark
Wants reasoning on why a move is illegal even if it creates a middle line
- question_mark
Checks understanding of endpoint vs middle cells in Array plus Matrix pattern
常见陷阱
外企场景- error
Only checking one or two directions instead of all eight
- error
Mistaking middle cell placement for endpoint legality
- error
Not handling board boundaries correctly when scanning lines
进阶变体
外企场景- arrow_right_alt
Check if multiple moves are legal in batch
- arrow_right_alt
Support boards larger than 8x8 with same good line rules
- arrow_right_alt
Determine all legal moves for a given color on the current board