999. Available Captures for Rook

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.

这道题LeetCode给出的是easy。
乍一看感觉复杂,明白后就发现,遍历R所在的行和列就好了,写完之后,代码通过了,但我发现我的代码一点都不简洁,竟然写了58行。。。

看了discuss,发现一个人用了函数,代码意思还是一样的,但是简洁了不少,很好!

猜你喜欢

转载自blog.csdn.net/maomao_dreamhigh/article/details/88681953