Consider an n?×?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.
You're given a painted grid in the input. Tell Lenny if the grid is convex or not.
Input The first line of the input contains two integers n and m (1?≤?n,?m?≤?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 【DFS|CodeForces - 275B (广搜)】On the only line of the output print "YES" if the grid is convex, otherwise print "NO". Do not print quotes.
Example Input
3 4 WWBW BWWW WWWB
Output
NO
Input
3 1 B B W
Output
YES
BFS ,从所有的黑色方格为起点出发,依次推出下一步的位置并加入队列中,看是否有需要超过2步的黑色方格,如果有直接GG。
代码:
#include
#include
#include
#include
#include
#include
#include
推荐阅读
- 遇见蓝桥遇见你|小唐开始刷蓝桥(一)2020年第十一届C/C++ B组第二场蓝桥杯省赛真题
- DFS|使用DFS(深搜)遍历所有的序列所有的子组合(子序列)(排列组合中的组合)
- Pavel loves grid mazes(CodeForce 377A)
- 搜索|Wannafly模拟赛3-B 贝伦卡斯泰露(DFS)
- CodeForces|2018.1.27【CodeForces - 689B】解题报告(BFS)
- CodeForces 377A
- DFS|CodeForces - 1099D(树上贪心+DFS)
- Codeforces Round #336 (Div. 1) D. Power Tree
- BFS|Solve The Maze(codeforces)