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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Python代码示例,用于实现对草坪的自动灌溉喷头布局进行数学建模: ```python import math class LawnIrrigation: def __init__(self, length, width, radius): self.length = length self.width = width self.radius = radius # 计算草坪的行列数 self.rows = int(math.ceil(length / radius)) self.cols = int(math.ceil(width / radius)) # 计算每个喷头的位置 self.positions = [] for row in range(self.rows): for col in range(self.cols): x = (col + 0.5) * radius y = (row + 0.5) * radius if x <= width and y <= length: self.positions.append((x, y)) def get_irrigation_time(self, flow_rate): # 计算每个喷头需要喷水的时间 irrigation_time = [] for pos in self.positions: area = math.pi * self.radius ** 2 volume = area * 0.3 # 假设草坪每平方米需要喷水0.3立方米 time = volume / flow_rate irrigation_time.append(time) return irrigation_time def get_irrigation_frequency(self, total_time): # 计算每个喷头的喷水频率 irrigation_time = self.get_irrigation_time(1) # 假设喷水流量为1立方米/小时 total_irrigation_time = sum(irrigation_time) frequency = [time / total_irrigation_time * total_time for time in irrigation_time] return frequency ``` 在上面的代码中,`LawnIrrigation`类用于表示草坪灌溉的数学模型。在类的初始化方法中,首先计算草坪的行列数,然后计算每个喷头的位置。在`get_irrigation_time`方法中,根据喷头位置计算出每个喷头需要喷水的时间。在`get_irrigation_frequency`方法中,根据每个喷头需要喷水的时间计算出每个喷头的喷水频率。其中,假设喷水流量为1立方米/小时,草坪每平方米需要喷水0.3立方米。 可以根据实际需要调整上述代码,以满足不同草坪灌溉布局的数学建模需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值