1958.检查操作是否合法

解题思路

思路是对的,是否存在好线段,则需要从给定节点的8个方向出发,然后寻找是否存在。
方向的问题,则构建行列下标变化量,

C

注意在某个方向上的检查函数,可用step进行判断是否是当前节点的下一个节点,while控制边界。
如果当前节点的下一个节点是空格或者颜色等于当前节点,则false
如果不是当前节点的下一个节点,则判断颜色是否等于当前节点,反之则继续往前走。

class Solution {
public:
    bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
        auto check = [&](int dx, int dy) -> bool{
            // 下一个节点位置
            int x = rMove + dx;
            int y = cMove + dy;
            int step = 1;
            while(x >= 0 && x<8 && y >=0 && y < 8){
                if (step == 1){
                    if (board[x][y] == '.' || board[x][y] == color){
                        return false;
                    }
                }
                else{
                    if (board[x][y] == '.'){
                        return false;
                    }
                    if (board[x][y] == color){
                        return true;
                    }
                }
                step ++;
                x += dx;
                y += dy;
            }
            return false;
        };
        // int dx[8] = {};
        vector<int> dx = {1, 1, 0, -1, -1, -1, 0, 1};   // 行改变量
        vector<int> dy = {0, 1, 1, 1, 0, -1, -1, -1};   // 列改变量
        for (int k = 0; k < 8; k++){
            if (check(dx[k], dy[k])){
                // 检查该方向是否存在满足条件的
                return true;
            }
        }
        return false;
    }
};

c++中判断两个字符是否相等,可直接用==
判断两个字符串的大小,则使用strcmp()

Java

class Solution {
    public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
        int[] dx = {1, 1, 0, -1, -1, -1, 0, 1};
        int[] dy = {0, 1, 1, 1, 0, -1, -1, -1}; 
        for (int i = 0; i < 8; i++){
            if (check(board, rMove, cMove, color, dx[i], dy[i])){
                return true;
            }
        }
        return false;

    }

    public boolean check(char[][] board, int rMove, int cMove, char color, int dx, int dy){
        int x = rMove + dx;
        int y = cMove + dy;
        int step = 1;
        while (x >= 0 && x < 8 && y >= 0 && y < 8){
            if (step == 1){
                if (board[x][y] == '.' || board[x][y] == color){
                    return false;
                }
            }
            else{
                if (board[x][y] == '.') return false;
                if (board[x][y] == color) return true;
            }
            step ++;
            x += dx;
            y += dy;
        }
        return false;
    }

}

Python

class Solution:
    def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
        def check(dx:int, dy:int)->bool:
            x, y = rMove + dx, cMove + dy
            step = 1
            while 0 <= x < 8 and 0 <= y < 8:
                if step == 1:
                    if board[x][y] == '.' or board[x][y] == color:
                        return False
                else:
                    if board[x][y] == '.':
                        return False
                    if board[x][y] == color:
                        return True
                step += 1
                x += dx
                y += dy
            return False
        dx = [1, 1, 0, -1, -1, -1, 0, 1]   # 行改变量
        dy = [0, 1, 1, 1, 0, -1, -1, -1]   # 列改变量
        for i in range(8):
            if check(dx[i], dy[i]):
                return True
        return False
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值