The Separator in Grid_BFS

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

    }
    return 0;
}
时间: 2024-10-29 19:10:59

The Separator in Grid_BFS的相关文章

UITableView去除空白cell上多余separator

具体的效果可以参考微信ios7版的UITableview 它最后一行cell的separator是顶到最左边的 首先设置tableFooterView [objc] view plaincopy _messageTableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 然后在willDisplayCell上增加如下代码 控制最后一行separatorInset位置 [objc] view plaincopy - 

java.io.File中的pathSeparator与separator的区别

先总的说一下区别:File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如:java   -cp   test.jar;abc.jar   HelloWorld就是指“;” File.separator才是用来分隔同一个路径字符串中的目录的,例如:C:\Program Files\Common Files就是指“\” separatorChar public static final char separatorChar 与系统有关的默认名称分隔符.此字段被初始化为包含系统

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

问题:当TableView的cell不能显示完整个屏幕(屏幕有剩余),则没有显示cell的地方会显示分割线.而正常情况下,如果没有cell则应没有分割线.如下图所示:左图为遇到问题,右图为想要的结果 解决方法: 1 我们可以通过代码禁止所有的cell显示分割线,即 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;然后再自定义cell,在cell里面添加一条线. 不过这种太麻烦 2 利用设置Separator边界可

makefile missing separator. Stop

1 ifneq ($(KERNELRELEASE),) 2 obj-m := hello.o 3 4 else 5 PWD := $(shell pwd) 6 KVER := $(shell uname -r) 7 KDIR := /lib/modules/$(KVER)/build 8 all: 9 $(MAKE) -C $(KDIR) M=$(PWD) modules 10 clean: 11 rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions 12 e

[zhuan]Android 异常处理:java.lang.IllegalArgumentException(...contains a path separator)

http://blog.csdn.net/alex_zhuang/article/details/7340901 对以下错误: Java.lang.RuntimeException: java.lang.IllegalArgumentException: File /data/data/com.alex.datasave/files/user.txt contains a path separator 原先代码: fis = this.context.openFileInput("/data/d

java输出换行的标准姿势&quot;line.separator&quot;

java中写.txt文件,实现换行的几种方法: 1.使用java中的转义符"\r\n": windows下的文本文件换行符:\r\n linux/unix下的文本文件换行符:\r Mac下的文本文件换行符:\n 1.String str="aaa"; 2.str+="\r\n"; 2.BufferedWriter的newline()方法: FileOutputStream fos=new FileOutputStream("c;\\11

System.getProperty("line.separator")注意事项

在实现multipart/form-data的图片上传时,需要用\r\n来分隔每一行,在JAVA中实现multipart/form-data的图片上传时则可以使用System.getProperty("line.separator")来进行每一行的分割.但是如果要将代码用于Android中,则切记不能使用System.getProperty("line.separator")来进行每一行的分割,因为在Android中System.getProperty("

UITableViewCell里面separator的设置

最近cell显示的时候左边一直有15个像素的偏移,查了下面的方法 //1. 不管用 [self.tableView setSeparatorInset:UIEdgeInsetsZero]; // 2.效果不明显,并不能完全从第一个像素显示分割线 1 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

关于Java的File.separator

一.File类 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还有几个与separator类似的静态常量,与系统有关,在编程中应尽量使用. ps:File file = new File("G:"+ File.separator +"demo.txt"); File类是java.io包中唯一一个与文件本身操作有关的类,文件本身操作是指