Codeforces Round #168 (Div. 2) Convex Shape

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;

char map[55][55];
int n,m;
int vis2[55][55];
int minvis[55][55];
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

struct node
{
	int x,y;
	int dir;
	int t;
};

queue<node> q;

int global_check_once;

int bfs(int x,int y)
{
	node tmp,top;
	int tx,ty;
	tmp.t=0;tmp.x=x;tmp.y=y;
	tmp.dir=-1;
	while(!q.empty())q.pop();
	memset(vis2,0,sizeof(vis2));
	memset(minvis,0,sizeof(minvis));
	q.push(tmp);
	vis2[x][y]=1;
	minvis[x][y]=0;
	//cout<<"begin to search "<<x<<" "<<y<<endl;
	while(!q.empty())
	{
		top=q.front();
		q.pop();
		//cout<<"pop "<<top.x<<" "<<top.y<<" "<<top.dir<<" "<<top.t<<endl;
		for(int i=0;i<4;i++)
		{
			tx=top.x+dx[i];
			ty=top.y+dy[i];
			
			if(tx>=0 && tx<n && ty>=0 && ty<m &&
			   map[tx][ty]=='B')// && vis[tx][ty][tmp.t][i]==0
			{
				tmp.t=top.t;
				if(i!=top.dir)tmp.t++;
			   	tmp.x=tx;tmp.y=ty;
				tmp.dir=i;
				if(vis2[tx][ty])
				{
					if(minvis[tx][ty]<tmp.t)
					{
						//cout<<"ignore "<<tx<<" "<<ty<<endl;
						continue;
					}
				}
				q.push(tmp);
				//cout<<x<<","<<y<<"push "<<tx<<" "<<ty<<" "<<i<<" "<<tmp.t<<endl;
				//vis[tx][ty][i]=1;
				vis2[tx][ty]=1;
				minvis[tx][ty]=tmp.t;
			}
		}
	}
	for(int i=0;i<n;i++)
	  for(int j=0;j<m;j++)
	    if(map[i][j]=='B' && minvis[i][j]>2)
		{
			//cout<<"find >2 "<<"("<<x<<","<<y<<") "<<i<<" "<<j<<endl;
			return 0;
		}
	if(0==global_check_once)
	{
		for(int i=0;i<n;i++)
		  for(int j=0;j<m;j++)
		    if(map[i][j]=='B' && vis2[i][j]==0)
			{
			   //cout<<"find seperated "<<endl;
			   return 0;
			}
		global_check_once=1;
	}
	return 1;
}

int solve()
{
	global_check_once=0;
	for(int i=0;i<n;i++)
	  for(int j=0;j<m;j++)
	    if(map[i][j]=='B')
	    {
	    	if(0==bfs(i,j))return 0;
	    }
	return 1;
}

int main()
{
	//freopen("b_in.txt","r",stdin);
	//freopen("b_out.txt","w",stdout);
	while(cin>>n>>m)
	{
		for(int i=0;i<n;i++)
		  cin>>map[i];
		if(solve())cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	//system("pause");
	return 0;
}

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 mcharacters “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

本文链接:Codeforces Round #168 (Div. 2) Convex Shape

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:Rexdf,谢谢!^^


此条目发表在ACM分类目录,贴了标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。

*

:zsmilebig: :zsadbig: :zwiredbig: :zgreenhappy: more »

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据