1958.检查操作是否合法
链接:1958.检查操作是否合法
难度:Medium
标签:数组、枚举、矩阵
简介:给你两个整数 rMove 和 cMove 以及一个字符 color ,表示你正在执行操作的颜色(白或者黑),如果将格子 (rMove, cMove) 变成颜色 color 后,是一个 合法 操作,那么返回 true ,如果不是合法操作返回 false 。
题解 1 - python
- 编辑时间:2024-07-07
- 执行用时:45ms
- 内存消耗:16.27MB
- 编程语言:python
- 解法介绍:遍历所有方向。
n = 8
dirs2 = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
class Solution:
    def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
        recolor = 'W' if color == 'B' else 'B'
        for dir in dirs2:
            nx, ny = rMove + dir[0], cMove + dir[1]
            cnt = 0
            while 0 <= nx < n and 0 <= ny < n:
                if board[nx][ny] == recolor:
                    cnt += 1
                elif board[nx][ny] == color:
                    if cnt >= 1: return True
                    break
                elif board[nx][ny] == '.':
                    break
                nx += dir[0]
                ny += dir[1]
        return False