Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

999. Available Captures for Rook #86

Open
Tcdian opened this issue Mar 26, 2020 · 1 comment
Open

999. Available Captures for Rook #86

Tcdian opened this issue Mar 26, 2020 · 1 comment
Labels

Comments

@Tcdian
Copy link
Owner

Tcdian commented Mar 26, 2020

999. Available Captures for Rook

在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。

车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。

返回车能够在一次移动中捕获到的卒的数量。

Example 1

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: 
In this example the rook is able to capture all the pawns.

Example 2

Input: [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0
Explanation: 
Bishops are blocking the rook to capture any pawn.

Example 3

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: 
The rook can capture the pawns at positions b5, d6 and f5.

Note

  • board.length == board[i].length == 8
  • board[i][j] is either 'R', '.', 'B', or 'p'
  • There is exactly one cell with board[i][j] == 'R'
@Tcdian
Copy link
Owner Author

Tcdian commented Mar 26, 2020

Solution

  • JavaScript Solution
/**
 * @param {character[][]} board
 * @return {number}
 */
var numRookCaptures = function(board) {
    const direction = [-1, 0, 1, 0, -1];
    let result = 0;
    let Rx;
    let Ry;
    
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++) {
            if (board[i][j] === 'R') {
                Rx = i;
                Ry = j;
                break;
            }        
        }
    }
    
    for (let d = 0; d < 4; d++) {
        let step = 1;
        while (true) {
            const dx = Rx + step * direction[d];
            const dy = Ry + step * direction[d + 1];
            if (dx < 0 || dx >= 8 || dy < 0 || dy >= 8 || board[dx][dy] === 'B') {
                break;
            }
            if (board[dx][dy] === 'p') {
                result++;
                break;
            }
            step++;
        }
    }
    
    return result;
    
};

@Tcdian Tcdian removed the LeetCode label Apr 23, 2020
@Tcdian Tcdian added the Array label May 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant