CodeForces 275B Convex Shape(bfs)

B. Convex Shape
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
Examples
Input
Copy

3 4
WWBW
BWWW
WWWB

Output

NO

Input
Copy

3 1
B
B
W

Output

YES

题意:从任意一个黑格子到任意一个黑格子都可以互达,且方向变化不超过1次(只能走黑格子)
解析:
用bfs做,但是有一点需要注意就是方向问题,那么我们有个最优策略就是已经变向过的格子可以被没有变化过的再走一次,并重新纪录.

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=100;
char arr[N][N];
int vis[N][N];
int dir1[]={-1,0,0,+1};
int dir2[]={0,-1,+1,0};
struct node
{
    int x,y,c,dir;
};
int main()
{
    int n,m,cnt=0;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        for(int j=0;j<m;j++)
            if(arr[i][j]=='B')
                cnt++;
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(arr[i][j]=='B')
            {
                int ans=0;
                memset(vis,-1,sizeof vis);
                queue<node>q;
                node p={i,j,0,-1};
                q.push(p);
                vis[i][j]=0;
                while(!q.empty())
                {

                    node t=q.front();
                    q.pop();
                    for(int k=0;k<4;k++)
                    {
                        int xx=t.x+dir1[k];
                        int yy=t.y+dir2[k];
                        if((vis[xx][yy]==-1||vis[xx][yy]>=t.c)&&arr[xx][yy]=='B')
                        {
                            if(t.dir==-1||k==t.dir)
                            {
                                node tt={xx,yy,t.c,k};
                                q.push(tt);
                                vis[xx][yy]=t.c;
                            }
                            else if(t.c<1)
                            {
                                node tt={xx,yy,t.c+1,k};
                                q.push(tt);
                                vis[xx][yy]=t.c+1;
                            }
                        }
                    }
                }
                for(int o=0;o<n;o++)
                {
                    for(int l=0;l<m;l++)
                    {
                         if(vis[o][l]!=-1)
                            ans++;
                    }
                }
                if(cnt!=ans)
                {
                    cout<<"NO"<<endl;
                    return 0;
                }
            }
        }
    }
    cout<<"YES"<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值