3710.Convex Shape

Time Limit: 1s Memory Limit: 256MB

Consider an n \times m grid. Initially all the cells of the grid are colored white. Lenny has painted some of the cells (at least one) black. We call a painted grid convex if one can walk from any black cell to any another black cell using a path of side-adjacent black cells changing his direction at most once during the path. In the figure below, the left grid is convex while the right one is not convex, because there exist two cells which need more than one time to change direction in their path.

3710_1.png

You're given a painted grid in the input. Tell Lenny if the grid is convex or not.

Input Format(From the terminal/stdin)

The first line of the input contains two integers n and m (1 \le n,m \le 50) - the size of the grid. Each of the next n lines contains m characters "B" or "W". Character "B" denotes a black cell of the grid and "W" denotes a white cell of the grid.

It's guaranteed that the grid has at least one black cell.

Output Format(To the terminal/stdout)

On the only line of the output print "YES" if the grid is convex, otherwise print "NO". Do not print quotes.

Sample Input 1

Copy
3 4
WWBW
BWWW
WWWB
 · \n
    \n
    \n
    \n

Sample Output 1

Copy
NO
  \n

Sample Input 2

Copy
3 1
B
B
W
 · \n
 \n
 \n
 \n

Sample Output 2

Copy
YES
   \n

Submit

请先 登录

© 2025 FAQs