Farm Irrigation

H - Farm Irrigation

 

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

 
Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE
then the water pipes are distributed like
 
Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

Input

There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.

<b< dd="">

Output

For each test case, output in one line the least number of wellsprings needed.


Sample Input

2 2
DK
HF

3 3
ADC
FJK
IHE

-1 -1


Sample Output
2
3
题意:
有ABCDEFGHIJK11种土地块,绿色部分为土地,蓝色部分为管道,给你mXn块,管道联通的部分只需要一个水源,未联通的部分也需要一个水源,问要用几个水源。

方案一:将每一个土地快转换成3x3的格子,有管道部分标记为'*',否则标记为‘.’。接着一个广搜搞定。(虽然略蠢可是简单好理解)

如‘A’表示为

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int m,n,book[200][200];
int nextt[4][2]= {-1,0,0,1,1,0,0,-1};
char mp[200][200],s[55][55];
struct node
{
    int x,y;
};
void bfs(int x,int y)
{
    queue<node>q;
    node now,temp;
    int tx,ty;
    now.x=x;
    now.y=y;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        int o=0;
        for(int i=0; i<4; i++)
        {
            tx=now.x+nextt[i][0];
            ty=now.y+nextt[i][1];
            if(tx<0||ty<0||tx>=m*3||ty>=n*3||book[tx][ty])
                continue;
            if(mp[tx][ty]=='*')
            {
                book[tx][ty]=1;
                temp.x=tx;
                temp.y=ty;
                q.push(temp);
            }
            else
                o++;
            if(o==4)
                return ;
        }
    }
}
int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        if(n==-1&&m==-1)
            break;
        memset(book,0,sizeof(book));
        int i,j,k,l;
        for(i=0; i<m; i++)
            scanf("%s",s[i]);
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)
            {//转换。
                if(s[i][j]=='A')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='.';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='.';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='B')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='.';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='.';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='C')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='.';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='.';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='D')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='.';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='.';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='E')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='.';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='.';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='F')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='.';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='.';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='G')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='.';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='H')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='.';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='I')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='.';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='J')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='.';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
                if(s[i][j]=='K')
                {
                    mp[i*3][j*3]='.';
                    mp[i*3][j*3+1]='*';
                    mp[i*3][j*3+2]='.';
                    mp[i*3+1][j*3]='*';
                    mp[i*3+1][j*3+1]='*';
                    mp[i*3+1][j*3+2]='*';
                    mp[i*3+2][j*3]='.';
                    mp[i*3+2][j*3+1]='*';
                    mp[i*3+2][j*3+2]='.';
                }
            }
        int sum=0;
        for(i=0; i<m*3; i++)
            for(j=0; j<n*3; j++)
                if(mp[i][j]=='*'&&book[i][j]==0)
                {
                    bfs(i,j);
                    sum++;
                }
        printf("%d\n",sum);
    }
    return 0;
}

方案二:将土地块四个方向,有管道联通的为‘1’,无则是‘0’,我标记的方向为上,右,下,左(顺时针)。

搜索的时候只需注意如果所处的位置有管道,若要联通,需要下一步将去的位置有管道。例如:


若要联通则  0和2,1和3 必须都有管道。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int m,n,book[60][60];
int nextt[4][2]= {-1,0,0,1,1,0,0,-1};
int mp[11][4]= {{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
//方向上右下左。
char s[55][55];
void dfs(int x,int y)
{
    int tx,ty,j;
    for(int i=0; i<4; i++)//当前所处格子的四个方向。
    {
        tx=x+nextt[i][0];
        ty=y+nextt[i][1];
        if(tx<0||ty<0||tx>=m||ty>=n||book[tx][ty])
            continue;
        if(i==0)
            j=2;
        if(i==1)
            j=3;
        if(i==2)
            j=0;
        if(i==3)
            j=1;
        if(mp[s[x][y]-'A'][i]&&mp[s[tx][ty]-'A'][j])
        {
            book[tx][ty]=1;
            dfs(tx,ty);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        if(n==-1&&m==-1)
            break;
        memset(book,0,sizeof(book));
        int i,j,k,l;
        for(i=0; i<m; i++)
            scanf("%s",s[i]);
        int sum=0;
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)
                if(book[i][j]==0)
                {
                    book[i][j]=1;
                    dfs(i,j);
                    sum++;
                }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gjlfly/article/details/80628475