Description

Given a connected, undirected graph G = (V, E), where V is the vertex set consisting a collection of nodes, and E is the set of edges, each of which connects two nodes from V. A vertex subset S is a separator if the subgraph induced by the vertices in V, but not in S, has two connected components. We shall use the notation [S, W, B] to represent the partition, where the removal of the separator S will give two connected components W and B.

In this problem, we consider the separators in grids. Each node in a grid is connected to its eight neighbors (if they exist). In Figure-1, we illustrate a partition of a 6*6 grid with a 9-point separator (gray nodes in the figure). The nodes on the left of the separator are in set W (white nodes), and the nodes on the right of the separator are in set B (black nodes). 

To simplify the problem, you can assume that all the separators referred in this problem satisfy the following restrictions: 
1) It’s a minimal separator. A separator is minimal if no subset of it forms a separator. 
2) It begins from a node on the top line of the grid, except the corner (i.e. 30 and 35 in the figures), and ends with a node on the bottom line of the grid, also except the corner (i.e. 0 and 5 in the figures). 
3) On its way from top to bottom, it can go left, right or down, but never go up.

Now we describe a method to improve a given partition on a grid, through which we can reduce the number of nodes in the separator. This method contains two steps: 
1) Select several nodes from B and add them into S. Any of the selected nodes must have a left neighbor which is in S. 
2) Remove several nodes from S (excluding the nodes added in the former step), and add them into W.

After the improvement, we should ensure S is still a separator, and make the number of nodes in S as small as possible. As for Figure-1, we should add 14 and 20 into S, and remove 7, 13, 19 and 25 from S. After that, we obtain a new partition with a 7-point separator shown in Figure-2.

Your task is, given a partition on a grid, to determine the least number of nodes in the separator after the improvement.

Input

There are several test cases. Each case begins with a line containing two integers, N and M (3 <= M, N <= 200). In each of the following N lines, there are M characters, describing the initial partition of the M*N grid. Every character is 'S', 'W' or 'B'. It is confirmed that each of these three characters appears at least once in each line, and 'W's are always on the left of 'S's.

A test case of N = 0 and M = 0 indicates the end of input, and should not be processed.

Output

For each test case, you should output one line containing one integer, which is the least number of nodes in the separator after the improvement.

Sample Input

6 6
WWSBBB
WSSBBB
WSBBBB
WSBBBB
WSSSBB
WWWSBB
0 0

Sample Output

7

【题意】给出一个n*m的矩阵,包含w,s,b,s是分界线,每行每种都至少有一个,B在他的左边是S时能变成S,S无条件可以变成w,求最少的分界线s

【思路】先把能变成S的B全部变成s,然后进行BFS从第一行的S开始,把(0,s)(0,s+1)入队,进行三个方向的搜索下、左、右

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int inf=0x7777777;
const int N=;
int n,m,ans,s;
char mp[N][N];
int vis[N][N];
int di[][]={,,,,,-};//以左上角为原点,下,右,左开始搜索
struct node
{
int x,y;
int step;
};
bool go(int x,int y)
{
if(x<||x>n-||y<||y>m-) return false;
else return true;
}
void bfs()
{
memset(vis,,sizeof(vis));
queue<node>qu;
node pre,next;
pre.x=,pre.y=s;
pre.step=;
qu.push(pre);
vis[][s]=;
if(s+<m-)
{
pre.x=;pre.y=s+;
pre.step=;
qu.push(pre);
vis[][s+]=;
}
while(!qu.empty())
{
pre=qu.front();
qu.pop();
if(pre.x==n-&&pre.y>&&pre.y<m-)
{
ans=min(ans,pre.step);
}
for(int i=;i<;i++)
{
int xx=pre.x+di[i][];
int yy=pre.y+di[i][];
if(go(xx,yy)&&(!vis[xx][yy])&&mp[xx][yy]=='S')
{
next.x=xx;
next.y=yy;
next.step=pre.step+;
vis[xx][yy]=;
qu.push(next);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m),n||m)
{
for(int i=;i<n;i++)
{
scanf("%s",mp[i]);
int flag=;
for(int j=;j<m;j++)
{
if(mp[i][j]=='B'&&mp[i][j-]=='S'&&!flag)
{
mp[i][j]='S';
flag=;
}
}
}
for(int i=;i<m;i++)
{
if(mp[][i]=='S')
{
s=i;
break;
}
}
ans=inf;
bfs();
printf("%d\n",ans); }
return ;
}

The Separator in Grid_BFS的更多相关文章

  1. UITableViewCell里面separator的设置

    最近cell显示的时候左边一直有15个像素的偏移,查了下面的方法 //1. 不管用 [self.tableView setSeparatorInset:UIEdgeInsetsZero]; // 2. ...

  2. ios7 ios8 cell中下划线偏移(separator Insets)处理方法

    在ios7中,UITableViewCell左侧会有默认15像素的空白.这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. 但是在ios8中,设置setS ...

  3. java.io.File中的pathSeparator与separator的差异

    先总的说一下区别: File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如: java -cp test.jar;abc.jar HelloWorld 就是指";&q ...

  4. oc TableView 分割线(separator)部分显示问题

    问题:当TableView的cell不能显示完整个屏幕(屏幕有剩余),则没有显示cell的地方也会显示分割线,这不是我们想要的,正常情况下,如果没有cell则应没有分割线.如下图所示:左图为遇到问题, ...

  5. UITableViewCell的separator分隔线设置失效

    // 处理separator -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSe ...

  6. 如何更改tableView cell的accessoryView位置,如何让首尾的Separator不显示

    一,如何更改tableView cell的accessoryView位置 1.实则是更改不了的,因此右边总会有一个小边距. 2.可以向 cell 的 contentView 中添加按钮放在右边,与 c ...

  7. [ASM C/C++] C makefile:2: *** missing separator. Stop. 问题

    在利用make编译代码时,makefile文件的目标代码前面要用tab而不能用空格来代替. 要不然就会提示: makefile:2: *** missing separator.  Stop. 要注意 ...

  8. 千份位Javascript Thousand Separator / string format

    function Separator(str){ return str.split(/(\d+)(\d{3})(\d{3})(\d{3})(\d{3})/).join(',').replace(/^, ...

  9. 关于Java的File.separator

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...

随机推荐

  1. Servlet后续的尾(yi)巴--------Filter过滤器

    -------载录自  http://www.blogjava.net/yangaiyou/archive/2007/08/29/140854.html  感谢博主心如止水 一心向佛 描写出这么的具体 ...

  2. placehold.it-在线图片生成器(转载)

    做网站的时候 如果 有的产品等客户没有上传图片,可以用这个网站生成的图片 并配以文字进行图片的占位 以免造成页面的空挡或者页面错位等 原文地址:http://www.cnblogs.com/xumen ...

  3. Struts2动态结果(${})and全局结果(global-results)

    动态结果 例:根据判定动态结果区分用户进行登录 1.先在TestAction类中进行判定赋值 public class TestAction extends ActionSupport { priva ...

  4. Ansible :一个配置管理和IT自动化工具

    编译文章:LCTT  https://linux.cn/article-4215-1.html 译者: felixonmars 文章地址:https://linux.cn/article-4215-1 ...

  5. Linux学习之CentOS--CentOS6.4下Mysql数据库的安装与配置【转】

      如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--C ...

  6. [Js]Ajax

    一.什么是Ajax 不刷新的情况下读取数据或提交数据 (最早出现ajax:谷歌地图,拖动一下出现一片新的视野) 应用:用户注册.在线聊天.微博 特性:只能从服务器上去读取数据(所以我们需要配置自己的服 ...

  7. Oracle创建表

    //创建表,列的内容 -- Create tablecreate table T_HQ_PC( pinpai VARCHAR2(20) not null, xingh VARCHAR2(40), ji ...

  8. 知名杀毒软件Mcafee(麦咖啡)个人版 资源汇总兼科普(来自卡饭)

    虽然早已不是用咖啡了,但我也实时关注的咖啡的一举一动,潜水看帖日久,发现小白众多,好多有价值的帖子淹没于帖海当中,甚是惋惜.     我有如下建议      1.咖啡区管理层,能否吧一些优秀的资源教程 ...

  9. COleDateTime类型的应用

    使用COleDateTime类1) 获取当前时间.      CTime time;      time = CTime::GetCurrentTime();2) 获取时间元素.      int y ...

  10. 【STL】-list的用法

    初始化: #include <list> list<char> clist; 算法: clist.push_back(c); clist.remove('d'); 代码: #i ...