Star Wars
InterviewTime limit1sMemory limit2048 MB
Pick one white piece on a grid so that, moving up one row at a time without landing on other white pieces, it captures the most black pieces.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
Amirreza is playing the Star Wars game on an board. Each cell is empty (.), contains a white piece (W), or contains a black piece (B). At the start, Amirreza chooses exactly one white piece to play with. He then moves this piece as many times as needed to knock out as many black pieces as possible.
If the white piece is at cell , one move takes it to , , or . The target cell must be on the board and must not contain another white piece. If the target cell contains a black piece, that piece is knocked out. Find the maximum number of black pieces Amirreza can knock out.
Input
The first line contains two integers and (), the number of rows and columns. The next lines each contain characters. The -th character of the -th line describes cell . Each character is W, B, or ., meaning a white piece, a black piece, or an empty cell.
Output
Print one integer, the maximum number of black pieces Amirreza can knock out.