leetcode999-车的可用捕获量

原题

999. 车的可用捕获量 - 力扣(LeetCode)

解法

思想

这道题题目有点难理解,其实就是上下左右搜索,质量有点低了。

代码

class Solution {
    char[][] boardGlobal;
    public int numRookCaptures(char[][] board) {
        boardGlobal = board;
        int originX,originY;
        originX = originY = 0;
        for(int i = 0;i<8;i++){
            for(int j = 0;j<8;j++){
                if(board[i][j]=='R'){
                    originX = i;
                    originY = j;
                }
            }
        }
        return search(originX,originY,1,1)+search(originX,originY,1,-1)+search(originX,originY,0,1)+search(originX,originY,0,-1);
    }
    //horizal是否水平,speed正向还是反向
    public int search(int originX,int originY,int horizal,int speed){
        while(true){
            originX += (horizal*speed);
            originY += ((horizal==1?0:1)*speed);
            if(originX==-1||originX==8||originY==-1||originY==8||boardGlobal[originX][originY]=='B'){
                return 0;
            }
            if(boardGlobal[originX][originY]=='p'){
                return 1;
            }
        }
    }
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode999-%e8%bd%a6%e7%9a%84%e5%8f%af%e7%94%a8%e6%8d%95%e8%8e%b7%e9%87%8f/