ZOJ 2412 Farm Irrigation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35323001/article/details/58189391

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2412

题目:

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

题目大意:

按照题目给的规格,分析农田里面有几个联通块

题目分析:

DFS,代码有点长,每个情况都要单独考虑

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m;
const int maxn=55;
char s[maxn][maxn];
int dx[]={0,0,1,-1};//右,左,下,上
int dy[]={1,-1,0,0};
void dfs(int x,int y)
{
    if(s[x][y]=='A')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
            }
        }
    }
    else if(s[x][y]=='B')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
            }
        }
    }
    else if(s[x][y]=='C')
    {
        s[x][y]='*';
       for(int i=0;i<4;i++)
       {
           int sx=x+dx[i],sy=y+dy[i];
           if(sx>=0&&sx<n&&sy>=0&&sy<m)
           {
               if(i==1)
               {
                   if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
               else if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
           }
       }
    }
    else if(s[x][y]=='D')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    else if(s[x][y]=='E')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    else if(s[x][y]=='F')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
            }
        }
    }
    else if(s[x][y]=='G')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
            }
        }
    }
    else if(s[x][y]=='H')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else  if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    else if(s[x][y]=='I')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else  if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    else if(s[x][y]=='J')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else  if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    else if(s[x][y]=='K')
    {
        s[x][y]='*';
        for(int i=0;i<4;i++)
        {
            int sx=x+dx[i],sy=y+dy[i];
            if(sx>=0&&sx<n&&sy>=0&&sy<m)
            {
                if(i==1)//左
                {
                    if(s[sx][sy]=='B'||s[sx][sy]=='D'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==3)//上
                {
                    if(s[sx][sy]=='C'||s[sx][sy]=='D'||s[sx][sy]=='E'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else if(i==0)//右
                {
                    if(s[sx][sy]=='A'||s[sx][sy]=='C'||s[sx][sy]=='F'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='I'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
                }
                else  if(i==2)//下
               {
                   if(s[sx][sy]=='A'||s[sx][sy]=='B'||s[sx][sy]=='E'||s[sx][sy]=='G'||s[sx][sy]=='H'||s[sx][sy]=='J'||s[sx][sy]=='K')
                    {
                        dfs(sx,sy);
                    }
               }
            }
        }
    }
    return ;
}

int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(n<=0||m<=0)
            break;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
        }
        int sum=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]!='*')
                {
                    sum++;
                    dfs(i,j);
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35323001/article/details/58189391