C - 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.

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

题意:

给你一个矩阵,矩阵中的字母代表水管的走向,问你要打多少井才能把农田灌溉完。

思路:

首先我们想到的肯定是如何把图给数字化方便我们搜索,我的第一反应就是把每一个字母都转换成一个小的矩阵,然后在把小的矩阵拼接成大的矩阵,然后我们用深搜搜索整个大的矩阵中水管分成了几块,然后块数就是我们要求的结果。

这道题就难在小的矩阵转换成大的矩阵。

小的矩阵我用了二维矩阵,有水管的地方就是1,没有水管的地方就是0.

这是例子中的转换出来的大的矩阵:

2 2
DK
HF

000010
011111
010010
010000
110111
010000

3 3
ADC
FJK
IHE

010000000
110011110
000010010
000010010
111011111
000010010
000010010
111110010
010010010

代码如下:

#include<stdio.h>
#include<string.h>

int n,m;
char map[51][51];
int a[200][200];
int next[11][9]=   //有水管的地方是1,没有水管的地方是0;
{
    {0,1,0,  //  A
     1,1,0,
     0,0,0},
    
    {0,1,0,   //   B
     0,1,1,
     0,0,0},
    
    {0,0,0,//   C
     1,1,0,
     0,1,0},
    
    {0,0,0,//   D
     0,1,1,
     0,1,0},

    {0,1,0,//   E
     0,1,0,
     0,1,0},
    
    {0,0,0,//   F
     1,1,1,
     0,0,0},
    
    {0,1,0,//   G
     1,1,1,
     0,0,0},
    
    {0,1,0,//    H
    1,1,0,
    0,1,0},

    {0,0,0,//    I 
     1,1,1,
     0,1,0},
    
    {0,1,0,//     J 
    0,1,1,
    0,1,0},
    
    {0,1,0, //   K
     1,1,1,
     0,1,0}
};

int nextt[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};

void dfs(int aa,int bb)
{
    a[aa][bb]=0;//搜过的地方都变成0;
    for(int i=0; i<4; i++)   //搜索四个方向;
    {
        int tx=aa+nextt[i][0];
        int ty=bb+nextt[i][1];
        if((tx>=0&&tx<n*3)&&(ty>=0&&ty<m*3)&&(a[tx][ty]==1))//判断是否越界;
            dfs(tx,ty);
    }
    return ;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==-1&&m==-1)
            break;
        int i,j;
        for(i=0; i<n; i++)
        {
            scanf("%s",map[i]);
        }
        for(i=0; i<n; i++)     //组合成大的矩阵;
        {
            for(j=0; j<m; j++)
            {
                int k=map[i][j]-'A';
                a[i*3][j*3]=next[k][0];
                a[i*3][j*3+1]=next[k][1];
                a[i*3][j*3+2]=next[k][2];
                a[i*3+1][j*3]=next[k][3];
                a[i*3+1][j*3+1]=next[k][4];
                a[i*3+1][j*3+2]=next[k][5];
                a[i*3+2][j*3]=next[k][6];
                a[i*3+2][j*3+1]=next[k][7];
                a[i*3+2][j*3+2]=next[k][8];
            }
        }
        /*for(i=0; i<n*3; i++)//输出大的矩阵;
        {
            for(j=0; j<m*3; j++)
            {
                printf("%d",a[i][j]);
            }
            printf("\n");
        }*/
        int sum=0;
        for(i=0; i<n*3; i++)
        {
            for(j=0; j<m*3; j++)
            {
                if(a[i][j]==1)
                {
                    dfs(i,j);
                    sum++;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81195167