Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of available capture of vehicles in leetcode

Shulou Source: shulou.com Published: 2022-06-01 05:03:19 09月30日 Update

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!

Tags: Direction movement catch character box white example explanation input output question chessboard article selection up and down up and down position only international chess Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft NVidia MariaDB Apple