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;
    }
}
发布了170 篇原创文章 · 获赞 18 · 访问量 6349
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览