HDU 1198 Farm Irrigation (DFS)

这篇博客讲述了如何解决一个关于农场灌溉的问题,其中涉及到不同类型的水管分布在一个矩形农田上。作者通过提供一个地图示例解释了水管的分布,并指出需要确定至少需要多少个水源才能灌溉整个农田。问题转化为计算相互连通的图形数量,作者采用DFS算法来求解,并给出了AC代码实现。
摘要由CSDN通过智能技术生成

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

题意描述:
给出由字符组成的矩阵和字符对应的图,输出相互连通的图有多少个。
解题思路:
我是把一个字符按照所对应的图转化为3*3的01矩阵,然后用dfs查找联通个数即可。
AC代码:

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

void dfs(int sx,int sy,int step);
int map[1210][1210],book[1210][1210];int m,n;
int main()
{
    char s[560][560];

    while(scanf("%d%d",&m,&n)!=EOF)
    {
        int i,j,sum=0;
        char ch;
        if(m<0||n<0)
            break;
        memset(map,0,sizeof(map));
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf(" %c",&ch);
                if(ch=='A')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=1;
                }
                else if(ch=='B')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-1]=map[i*3-1][j*3]=1;
                }
                else if(ch=='C')
                {
                    map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3][j*3-1]=1;
                }
                else if(ch=='D')
                {
                    map[i*3-1][j*3-1]=map[i*3-1][j*3]=map[i*3][j*3-1]=1;
                }
                else if(ch=='E')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-1]=map[i*3][j*3-1]=1;
                }
                else if(ch=='F')
                {
                    map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3-1][j*3]=1;
                }
                else if(ch=='G')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3-1][j*3]=1;
                }
                else if(ch=='H')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3][j*3-1]=1;
                }
                else if(ch=='I')
                {
                    map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3-1][j*3]=map[i*3][j*3-1]=1;
                }
                else if(ch=='J')
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3]=map[i*3-1][j*3-1]=map[i*3][j*3-1]=1;
                }
                else
                {
                    map[i*3-2][j*3-1]=map[i*3-1][j*3-2]=map[i*3-1][j*3-1]=map[i*3][j*3-1]=map[i*3-1][j*3]=1;
                }
            }
        }
        memset(book,0,sizeof(book));
        int sx=0,sy=0;
        for(i=1;i<=m*3;i++)
        {
            for(j=1;j<=n*3;j++)
            {
                if(map[i][j]==1&&book[i][j]==0)
                {
                    sx=i;sy=j;
                    book[sx][sy]=1;
                    dfs(sx,sy,0);
                    sum++;
                }
            }

        }
        printf("%d\n",sum);
    }
    return 0;
}
void dfs(int sx,int sy,int step)
{
    int tx,ty;
    int i,j;
    int next[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    for(i=0;i<4;i++)
    {
        tx=sx+next[i][0];
        ty=sy+next[i][1];
        if(tx<=0||tx>m*3||ty<=0||ty>n*3)
        {
            continue;
        }
        if(map[tx][ty]==1&&book[tx][ty]==0)
        {
            book[tx][ty]=1;
            dfs(tx,ty,step+1);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值