接外包,有相关需求的可以联系我:Telegram | Email

LeetCode: 999. Available Captures for Rook

该文章创建(更新)于09/12/2020,请注意文章的时效性!

Description

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.

Example

Example 1:

  • 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'

思想

  • step1:找到R的位置board[i][j]
  • step2:从R的位置依次向上下左右四个方向尝试,碰到第一个字符若为p则可以向此处移动,即可移动的数量加一,其它任何情况都退出寻找。则可得到我们需要的值;

Code

JAVA

提交代码

class Solution {
 public int numRookCaptures(char[][] board) {
            //find the location of R
            int m = 0,n = 0,b = 0;// break all if b = 1
            for(m = 0 ; m < 7; m++ ){
                for(n = 0 ; n < 7; n++){
                    if (board[m][n] == 'R'){
                        b = 1;
                        break;
                    }
                }

                if(b == 1){
                    break;
                }
            }

            int count = 0;// home much way The R could take?

            //find the location B or p in x xary;
            int i = n - 1;
            while(i >= 0){
                if(board[m][i] == 'p'){
                    count++;
                    break;
                }else if(board[m][i] == 'B'){
                    break;
                }
                i--;
            }
            i = n + 1;
            while(i <= 7){
                if(board[m][i] == 'p'){
                    count++;
                    break;
                }else if(board[m][i] == 'B'){
                    break;
                }
                i++;
            }



            //find the location B or p in y xary;
            int j = m - 1;
            while(j >= 0){
                if(board[j][n] == 'p'){
                    count++;
                    break;
                }else if(board[j][n] == 'B'){
                    break;
                }
                j--;
            }
            j = m + 1;
            while(j <= 7){
                if(board[j][n] == 'p'){
                    count++;
                    break;
                }else if(board[j][n] == 'B'){
                    break;
                }
                j++;
            }

            return count;
        }
}

本地测试完整代码

public class LeetCode_Two {
    static class Solution {
        public int numRookCaptures(char[][] board) {
            //find the location of R
            int m = 0,n = 0,b = 0;// break all if b = 1
            for(m = 0 ; m < 7; m++ ){
                for(n = 0 ; n < 7; n++){
                    if (board[m][n] == 'R'){
                        b = 1;
                        break;
                    }
                }

                if(b == 1){
                    break;
                }
            }

            int count = 0;// home much way The R could take?

            //find the location B or p in x xary;
            int i = n - 1;
            while(i >= 0){
                if(board[m][i] == 'p'){
                    count++;
                    break;
                }else if(board[m][i] == 'B'){
                    break;
                }
                i--;
            }
            i = n + 1;
            while(i <= 7){
                if(board[m][i] == 'p'){
                    count++;
                    break;
                }else if(board[m][i] == 'B'){
                    break;
                }
                i++;
            }



            //find the location B or p in y xary;
            int j = m - 1;
            while(j >= 0){
                if(board[j][n] == 'p'){
                    count++;
                    break;
                }else if(board[j][n] == 'B'){
                    break;
                }
                j--;
            }
            j = m + 1;
            while(j <= 7){
                if(board[j][n] == 'p'){
                    count++;
                    break;
                }else if(board[j][n] == 'B'){
                    break;
                }
                j++;
            }

            return count;
        }
    }

    public static void main(String[] args) {
        //char must be ' ',not " "
            char [][] a = new char[][]{{'.', '.', '.', '.', '.', '.', '.', '.'},
                    {'.', '.', 'B', 'B', 'B', 'B', 'B', '.'},
                    {'.', 'p', 'B', 'p', 'p', 'p', 'B', 'p'},
                    {'.', 'p', 'B', 'p', 'R', 'p', 'B', 'p'},
                    {'.', 'p', 'B', 'p', 'p', 'p', 'B', 'p'},
                    {'.', '.', 'B', 'B', 'B', 'B', 'B', '.'},
                    {'.', '.', '.', 'p', 'p', 'p', '.', '.'},
                    {'.', '.', '.', '.', '.', '.', '.', '.'}};

        Solution S = new Solution();
        System.out.println(S.numRookCaptures(a));


    }
}

Python3

upload Code

class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
        #find the location of R
        m = 0
        n = 0
        b = 0# break all if b = 1
        while m < 7:
            n = 0
            while n < 7:
                if (board[m][n] == 'R'):
                        b = 1
                        break
                n += 1                        
            if(b == 1):
                break
            m += 1    


        count = 0#home much way The R could take

        #find the location B or p in x xary;
        i = n - 1
        while i >= 0:
            if(board[m][i] == 'p'):
                count += 1
                break
            elif(board[m][i] == 'B'):
                    break                
            i -= 1

        i = n + 1
        while i <= 7:
            if(board[m][i] == 'p'):
                count += 1
                break
            elif(board[m][i] == 'B'):
                break        
            i += 1 



        #find the location B or p in y xary
        j = m - 1
        while j >= 0:
            if(board[j][n] == 'p'):
                count += 1
                break
            elif(board[j][n] == 'B'):
                break            
            j -= 1        
        j = m + 1
        while j <= 7:
            if(board[j][n] == 'p'):
                count += 1
                break
            elif(board[j][n] == 'B'):
                break
            j += 1

        return count

test code

class Solution:
    def numRookCaptures(self, board):
        #find the location of R
        m = 0
        n = 0
        b = 0# break all if b = 1
        while m < 7:
            n = 0
            while n < 7:
                if (list[m][n] == 'R'):
                        b = 1
                        break
                n += 1                        
            if(b == 1):
                break
            m += 1    


        count = 0#home much way The R could take

        #find the location B or p in x xary;
        i = n - 1
        while i >= 0:
            if(list[m][i] == 'p'):
                count += 1
                break
            elif(list[m][i] == 'B'):
                    break                
            i -= 1

        i = n + 1
        while i <= 7:
            if(list[m][i] == 'p'):
                count += 1
                break
            elif(list[m][i] == 'B'):
                break        
            i += 1 



        #find the location B or p in y xary
        j = m - 1
        while j >= 0:
            if(list[j][n] == 'p'):
                count += 1
                break
            elif(list[j][n] == 'B'):
                break            
            j -= 1        
        j = m + 1
        while j <= 7:
            if(list[j][n] == 'p'):
                count += 1
                break
            elif(list[j][n] == 'B'):
                break
            j += 1

        return count


list = [[".",".",".",".",".",".",".","."],
        [".",".","B","B","B","B","B","."],
        [".","p","B","p","p","p","B","p"],
        [".","p","B","p","R","p","B","p"],
        [".","p","B","p","p","p","B","p"],
        [".",".","B","B","B","B","B","."],
        [".",".",".","p","p","p",".","."],
        [".",".",".",".",".",".",".","."]]

S = Solution()
print(S.numRookCaptures(list))

要不赞赏一下?

微信
支付宝
PayPal
Bitcoin

版权声明 | Copyright

除非特别说明,本博客所有作品均采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。转载请注明转自-
https://www.emperinter.info/2020/09/12/available-captures-for-rook/


要不聊聊?

我相信你准备留下的内容是经过思考的!【勾选防爬虫,未勾选无法留言】

*

*



YouTube | B站

微信公众号

👉 NewsLetter ❤️ 邮箱订阅 👈

优惠码

阿里云国际版20美元
Vultr10美元
搬瓦工 | Bandwagon应该有折扣吧?
Just My SocksJMS9272283 【注意手动复制去跳转】
域名 | namesiloemperinter(1美元)
币安 币安