CF275B.Convex Shape

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

考虑一个 n×mn \times m 的网格。初始时,网格中所有格子均为白色。Lenny 将其中一些格子(至少一个)涂成了黑色。若从任意一个黑格出发,总能通过一条仅经过黑格、且在路径中至多改变一次方向的侧向相邻(即上下左右相邻)路径到达任意另一个黑格,则称该涂色后的网格是凸的。如下图所示,左侧网格是凸的,而右侧网格不是凸的,因为存在两个黑格,它们之间的任意侧向相邻黑格路径都需要改变方向超过一次。

输入给出一个已涂色的网格,请判断该网格是否为凸的。

输入格式

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.

输入的第一行包含两个整数 nn 和 mm(1 ≤ n, m ≤ 501 ≤ n, m ≤ 50)——表示网格的大小。接下来的 nn 行中,每行包含 mm 个字符,每个字符为 "B" 或 "W"。字符 "B" 表示网格中的一个黑格,字符 "W" 表示网格中的一个白格。

保证网格中至少存在一个黑格。

输出格式

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

在输出的唯一一行中,如果网格是凸的,则打印 “YES”,否则打印 “NO”。不要打印引号。

输入输出样例

  • 输入#1

    3 4
    WWBW
    BWWW
    WWWB

    输出#1

    NO
  • 输入#2

    3 1
    B
    B
    W

    输出#2

    YES

输入解题思路,AI测评打分。不知道怎么写?

首页