LeetCode-3.26-999-E-车的可用捕获量(Available Captures for Rook )

文章目录


在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。
车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。
返回车能够在一次移动中捕获到的卒的数量。

On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters ‘R’, ‘.’, ‘B’, and ‘p’ respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.

The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.

Return the number of pawns the rook can capture in one move.

示例 1:
在这里插入图片描述
输入:[[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],[".",".",".",“R”,".",".",".",“p”],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。
示例 2:
在这里插入图片描述
输入:[[".",".",".",".",".",".",".","."],[".",“p”,“p”,“p”,“p”,“p”,".","."],[".",“p”,“p”,“B”,“p”,“p”,".","."],[".",“p”,“B”,“R”,“B”,“p”,".","."],[".",“p”,“p”,“B”,“p”,“p”,".","."],[".",“p”,“p”,“p”,“p”,“p”,".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:0
解释:
象阻止了车捕获任何卒。

思路

本题按照数学模拟的情况进行,整体分为找到“白车”和在“白车”的四个方向寻找“黑卒”和“白象”两个步骤。
(1)找“白车”。双重循环的遍历,没什么好解释。唯一需要注意的是如何跳出双重循环并记录下白车的坐标。这里使用的是 break label。另外一种使用布尔值控制,如下代码不可行,当flag == true后,j仍旧会进行+1的操作,然后进行下次循环判断时才不进入,同理i也会进行+1的操作。这样“白象”的坐标就变动了。

        boolean flag = false;
        for(i = 0; i < len && !flag; i++){
            for( j = 0; j < len && !flag; j++){
                if(board[i][j] == 'R'){
                    flag = true;
                }
            }
        }

(2)在“白车”的四个方向寻找“黑卒”和“白象”。以“白车”为中心点向四个方向进行搜索,需要白象就跳出当前方向,被阻挡,遇到黑卒就+1,然后跳出。

解法

在这里插入图片描述

class Solution {
    public int numRookCaptures(char[][] board) {
        int len = board.length;
        int i = 0, j = 0;
        // 1. find the white Rook
        label:
        for(; i < len; i++){
            for(j = 0; j < len; j++){ //error
                if(board[i][j] == 'R'){
                    // flag = true;
                    break label;
                }
            }
        }
        int count = 0;
        //2. find 4 directions
        for(int r1 = i; r1 >= 0; r1-- ){
            if(board[r1][j] =='B'){
                break;
            }
            if(board[r1][j] == 'p'){
                count++;
                break;
            }
        }
        
        for( int r2 = i; r2 < len; r2++){
            if(board[r2][j] =='B'){
                break;
            }
            if(board[r2][j] == 'p'){
                count++;
                break;
            }
        }
        
        for(int c1 = j; c1 >= 0; c1--){
            if(board[i][c1] =='B'){
                break;
            }
            if(board[i][c1] == 'p'){
                count++;
                break;
            }
        }
        
        for(int c2 = j; c2 < len; c2++){
            if(board[i][c2] =='B'){
                break;
            }
            if(board[i][c2] == 'p'){
                count++;
                break;
            }
        }
        
        return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值