In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to solve the problem of available car capture in leetcode. I hope you will get something after reading this article. Let's discuss it together.
The available catch of the car
On an 8 x 8 board, there is a white car (rook). There may also be empty squares, white elephants (bishop) and black pawn (pawn). They are given with the characters "R", ".", "B" and "p", respectively. Uppercase characters represent white chess and lowercase characters represent black chess.
The car moves according to the rules in chess: it chooses one of the four basic directions (north, east, west and south) and then moves in that direction until it chooses to stop, reach the edge of the chessboard, or move to the same square to capture the pawn of the opposite color on the square. In addition, the car cannot enter the same box as other friends (white).
Returns the number of paws that the car can capture in a single move.
Example 1:
Enter:
[[. ",".], [., "p", ".], [.", "R", ".", "p"], [. " ".], [.], [.,".], [., "p", ".], [. ". []
Output: 3
Explanation:
In this case, the car can capture all the pawn.
Example 2:
Enter:
[[".", ".], [.," p ",".], [. "," p "," p "," B "," p "," p ",". "], [.," p "," B "," R "," B "," p ",". " ".], [.," p "," p "," B "," p "," p ",". "], [.", "p", ". ". []
Output: 0
Explanation:
The elephant stopped the car from capturing any pawn.
Example 3:
Enter:
[[. ",".], [., "p", ".], [.", "p", ".], [" p "," p ",". "," R ",". "," p "," B " ".], [.], [.,".], [., "B", ".], [.", "p", ".], [. ". []
Output: 3
Explanation:
The car can capture the pawn of position b5, d6 and f5.
Tip:
Board.length = = Board [I] .length = = 8
Board [I] [j] can be 'Renewal dagger.' or'p'.
Only one lattice has board [I] [j] ='R'
Train of thought:
The title means the number of times R can eat p after walking in the middle, the direction is up and down, left and right.
The restriction is to walk once, and the encounter with B indicates that the direction is impassable.
Class Solution: def numRookCaptures (self, board: list [list [str]])-> int: cnt, st, ed = 0,0,0 # direction array direction = [(0,1), (0,1), (1,0), (- 1) 0)] # find R for i in range (8): for j in range (8): if board [I] [j] ='st: st = I ed = j # explore for i in range (4): step = 0 in four directions While True: dx Dy = direction [I] tx = st + step * dx ty = ed + step * dy if tx
< 0 or tx >= 8 or ty
< 0 or ty >= 8 or board [tx] [ty] = 'B': break if board [tx] [ty] =' paired: cnt + = 1 break step + = 1 # the range of each move, move one frame for the first time, if you can't find it, move two frames return cnt finished reading this article I believe you have a certain understanding of "how to solve the problem of available car capture in leetcode". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 262
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.