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

来源:互联网 发布:淘宝万事屋手办 编辑:程序博客网 时间:2024/05/29 14:47

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 gridconvex 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 andm (1 ≤ n, m ≤ 50) — the size of the grid. Each of the nextn 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 4WWBWBWWWWWWB
Output
NO
Input
3 1BBW
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