Farm Irrigation【DFS】

本文探讨了一种基于图论的算法解决农场灌溉问题,通过分析不同类型的水管布局,确定最少的水源点数量以确保整个农场得到充分灌溉。利用深度优先搜索(DFS)遍历地图,识别独立的灌溉区域。

Problem Description
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
 

题意:有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100;
char mapp[maxn][maxn];
bool vis[maxn][maxn];
int M,N;
int dir[12][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}
};//上,右,下,左
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
int judge(int x,int y)
{
    if(x>0&&x<=M&&y>0&&y<=N&&!vis[x][y])
        return true;

    else
        return false;
}
void dfs(int p,int x,int y)
{
    int num=mapp[x][y]-'A';//求出这个块是第几个
    int xx,yy;
    if(p<4)
        if(!dir[num][(p+2)%4])//判断是否连通
        {
            vis[x][y]=false;
            return;
        }
    for(int i=0; i<4; i++)
    {
        if(dir[num][i])
        {
            xx=x+dx[i];
            yy=y+dy[i];
            if(judge(xx,yy))
            {
                vis[xx][yy]=true;
                dfs(i,xx,yy);//i表示方向。
            }
        }
    }



}


int main()
{
    while(cin>>M>>N)
    {
        if(M==-1&&N==-1)
            break;
        for(int i=1; i<=M; i++)
            for(int j=1; j<=N; j++)
                cin>>mapp[i][j];
        memset(vis,false,sizeof(vis));
        int res=0;
        for(int i=1; i<=M; i++)
            for(int j=1; j<=N; j++)
                if(!vis[i][j])//遍历每一个位置,找出连通区域的数量。
                {
                    vis[i][j]=true;
                    res++;
                    dfs(maxn,i,j);
                }
        cout<<res<<endl;
    }

    return 0;
}

 

内容概要:本文介绍了一个关于超声谐波成像中幅度调制聚焦超声所引起全场位移和应变的分析模型,并提供了基于Matlab的代码实现。该模型旨在精确模拟和分析在超声谐波成像过程中,由于幅度调制聚焦超声作用于生物组织时产生的力学效应,包括全场的位移与应变分布,从而为医学成像和治疗提供理论支持和技术超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)手段。文中详细阐述了模型构建的物理基础、数学推导过程以及Matlab仿真流程,具有较强的理论深度与工程应用价值。; 适合人群:具备一定声学、生物医学工程或力学背景,熟悉Matlab编程,从事医学成像、超声技术或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中的力学建模与仿真分析;②支持高强度聚焦超声(HIFU)治疗中的组织响应预测;③作为教学案例帮助理解超声与组织相互作用的物理机制;④为相关科研项目提供可复用的Matlab代码框架。; 阅读建议:建议读者结合超声物理和连续介质力学基础知识进行学习,重点关注模型假设、偏微分方程的数值求解方法及Matlab实现细节,建议动手运行并修改代码以加深理解,同时可拓展应用于其他超声成像或治疗场景的仿真研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值