The Separator in Grid_bfs

Source: Internet
Author: User

Description

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

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

To simplify the problem, you can assume that all the separators referred in this problem satisfy the following restriction S
1) It ' s a minimal separator. A separator is minimal if no subset of it forms a separator.
2) It begins from a node to the top line of the grid, except the corner (i.e. and the figures), and ends with a no De on the bottom line of the grid, also except the corner (i.e. 0 and 5 in the figures).
3) on the its-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 SE Parator. This method contains the steps:
1) Select several nodes from B and add them to S. Any of the selected nodes must has a left neighbor which are 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 are still a separator, and make the number of nodes in S as small as possible. As for Figure-1, we should add to s, and remove 7, from S. After the, 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 is several test cases. Each case begins with a line containing the integers, N and M (3 <= m, n <= 200). In each of the following N lines, there is M characters, describing the initial partition of the M*n grid. Every character is ' S ', ' W ' or ' B '. It is confirmed this each of the these three characters appears at least once in all line, and ' W's is always on the left of ' s ' s.

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

Output

Should output one line containing one integer, which was the least number of nodes in the separator After the improvement.

Sample Input

6 6wwsbbbwssbbbwsbbbbwsbbbbwsssbbwwwsbb0 0

Sample Output

7

"Test Instructions" gives a n*m matrix, containing w,s,b,s is the dividing line, each row has at least one, B on his left is the s when it can become s,s unconditionally can become W, the least of the dividing lines s

"Idea" first turns the b that can turn S into S, then the BFS starts from the first line of S, the (0,s) (0,s+1) queue, the search for three directions under, left, right

#include <iostream>#include<stdio.h>#include<queue>#include<string.h>using namespacestd;Const intinf=0x7777777;Const intn= About;intn,m,ans,s;CharMp[n][n];intVis[n][n];intdi[3][2]={1,0,0,1,0,-1};//the top left corner as the origin, bottom, right, and left to search .structnode{intx, y; intstep;};BOOLGointXinty) {    if(x<0|| x>n-1|| y<0|| y>m-1)return false; Else return true;}voidBFs () {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;p re.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(intI=0;i<3; i++)        {            intxx=pre.x+di[i][0]; intyy=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); }        }    }}intMain () { while(~SCANF ("%d%d", &n,&m), n| |m) { for(intI=0; i<n;i++) {scanf ("%s", Mp[i]); intflag=0;  for(intj=0; j<m;j++)             {                 if(mp[i][j]=='B'&&mp[i][j-1]=='S'&&!flag) {Mp[i][j]='S'; Flag=1; }             }        }         for(intI=0; i<m;i++)        {            if(mp[0][i]=='S') {s=i;  Break; }} ans=inf;        BFS (); printf ("%d\n", ans); }    return 0;}

The Separator in Grid_bfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.