Codeforces Round #168 (Div. 2) B. Convex Shape

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

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

Sample test(s)
Input
3 4
WWBW
BWWW
WWWB
Output
NO
Input
3 1
B
B
W
Output
YES

题意: 从地图上任意一个黑格  到达另外的任意一个黑格,其中必须经过黑格且只能拐弯两次..

思路: 一开始想复杂了, 用了DFS+BFS, 会有各种意外情况发生, 其实数据那么小 完全可以使用暴力.

CODE:

#include<stdio.h>
#include<iostream>
using namespace std;
char map[55][55];
int n,m,x[3000],y[3000];

int check(int xx1,int yy1,int xx2,int yy2)
{
    for(int i=min(yy1,yy2);i<=max(yy1,yy2);i++)
        if(map[xx1][i]!='B') return 0;
    for(int i=min(xx1,xx2);i<=max(xx1,xx2);i++)
        if(map[i][yy2]!='B') return 0;
    return 1;
}
int main()
{
    cin>>n>>m;
    getchar();
    int k=-1;
    for(int i=0;i<n;i++)
    {
        cin>>map[i];
        for(int j=0;j<m;j++)
        {
            if(map[i][j]=='B') x[++k]=i,y[k]=j;
        }
        //getchar();
    }
    for(int i=0;i<k;i++)
        for(int j=i+1;j<=k;j++)
    {
        if(check(x[i],y[i],x[j],y[j])) continue;
        if(check(x[j],y[j],x[i],y[i])) continue;
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值