-
发表于 2024.07.07
-
直接枚举八个方向,如果有一个方向满足要求,直接返回
True
即可。如何判断是否满足要求呢?先记目标颜色color
为A
,相反颜色为B
,我们需要在至少一种方向的路径上满足AB..BA
格式。从落点(rMove, cMove)
出发,枚举八个方向,沿着方向一路检查,如果走出边界或者遇到空格,说明无法构成AB..BA
的格式,不满足要求;如果遇到相反颜色,则判断B
的个数是否大于等于1,如果是,则满足要求。class Solution: moves = ( (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1) ) def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: n = len(board) legal = False for move in self.moves: r, c = rMove, cMove middle_cnt = 0 while True: r, c = r + move[0], c + move[1] if r < 0 or r >= n or c < 0 or c >= n or board[r][c] == '.': legal = False break if board[r][c] != color: middle_cnt += 1 else: legal = middle_cnt >= 1 break if legal: break return legal
- LC 题目链接
-