hdu 1198 Farm Irrigation ( 打表+并查集)

本文介绍了一个农场灌溉模拟问题,通过并查集算法确定最少的水源点数以确保整个农场得到灌溉。涉及11种不同类型的水管布局,需要计算最优水源配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6432    Accepted Submission(s): 2787


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
 

Author
ZHENG, Lu
 

Source
题目分析:并查集裸题,就是模拟的部分麻烦一点
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 600

using namespace std;

bool connect[15][7];

void init ( )
{
    // 1 for up , 2 for riht , 3 for down , 4 for left 
    memset ( connect , false , sizeof ( connect ) );
    connect[1][1] = connect[2][1] = connect[5][1] = connect[7][1] = 1;
    connect[8][1] = connect[10][1] = connect[11][1] = 1;
    connect[2][2] = connect[4][2] = connect[6][2] = connect[7][2] = 1;
    connect[9][2] = connect[10][2] = connect[11][2] = 1;
    connect[3][3] = connect[4][3] = connect[5][3] = connect[8][3] = 1;
    connect[9][3] = connect[10][3] = connect[11][3] = 1;
    connect[1][4] = connect[3][4] = connect[6][4] = connect[7][4] = 1;
    connect[8][4] = connect[9][4] = connect[11][4] = 1;
}

int dx[]={0,-1,0,1,0};
int dy[]={0,0,1,0,-1};

int n,m;
int mp[MAX][MAX];
char s[107];

int get_id ( int x , int y )
{
    return (x-1)*m+y;
}

int fa[MAX*MAX];
bool used[MAX*MAX];

void init2 ( )
{
    for ( int i = 1 ; i < MAX*MAX ; i++ )
        fa[i] = i;
}

int find ( int x )
{
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void _union ( int x , int y )
{
    x = find(x);
    y = find(y);
    fa[x] = y;
}

int main ( )
{
    init( );
    while ( ~scanf ( "%d%d" , &n , &m ) )
    {
        init2();
        memset ( mp , -1 , sizeof ( mp ) );
        if ( n == -1 && m == -1 ) break;
        for ( int i = 1 ; i <= n ; i++ )
        {
            scanf ( "%s" , s+1 );
            for ( int j = 1 ; j <= m ; j++ )
                mp[i][j] = s[j]-64;
        }
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
                for ( int k = 1 ; k < 5 ; k++ )
                {
                    int tx = i+dx[k] , ty = j+dy[k];
                    int type2 = mp[tx][ty];
                    if ( type2 == -1 ) continue;
                    int type1 = mp[i][j];
                    if ( !connect[type1][k] ) continue;
                    int k1;
                    if ( k == 1 ) k1 = 3;
                    if ( k == 2 ) k1 = 4;
                    if ( k == 3 ) k1 = 1;
                    if ( k == 4 ) k1 = 2;
                    if ( connect[type2][k1] )
                        _union ( get_id(i,j) , get_id(tx,ty) );
                }
        memset ( used , 0 , sizeof ( used ) );
        int ans = 0;
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
            {
                int id = find( get_id(i,j) );
                if ( used[id] ) continue;
                used[id] = true;
                ans++;
            }
        printf ( "%d\n" , ans );
    }
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值