Farm Irrigation

**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4991 Accepted Submission(s): 2143

**

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

题目大意:给定农田的水管的走向,如果两块农田有水管能够互相连通,则它们是相连的,水流能通过两块农田。要你求出最少需要挖多少口井(水井在每块农田的正中央),才能使所有农田都被灌溉。根据上图例子,需要3口水井就能将所有农田都被灌溉。

分析:这道题有两种方法可以做,第一种是简单dfs,第二种是并查集,dfs如果不懂的同学就要去普及搜索知识了。并查集的话同样,求连通区域的个数,如果两块农田连通,则它们在一个等价类中,最后求等价类个数。

dfs方法实现如下

首先要对11个农田状态进行标记,个人标记的方法各不相同,可以使用二维数组存储,这里我用结构体表示更为直观:

#include<iostream>
#include<cstring>
using namespace std;
struct farm {
bool top, bottom, left, right;
farm() {
top = bottom = left = right = false;
}
}FM[13]; bool book[55][55];
char map[55][55];
int n, m; void init() {
FM[0].left = FM[0].top = true;
FM[1].right = FM[1].top = true;
FM[2].left = FM[2].bottom = true;
FM[3].right = FM[3].bottom = true;
FM[4].top = FM[4].bottom = true;
FM[5].left = FM[5].right = true;
FM[6].left = FM[6].right = FM[6].top = true;
FM[7].left = FM[7].top = FM[7].bottom = true;
FM[8].left = FM[8].right = FM[8].bottom = true;
FM[9].top = FM[9].right = FM[9].bottom = true;
FM[10].left = FM[10].right = FM[10].top = FM[10].bottom = true;
} void dfs(int x, int y)
{
book[x][y] = true;
int c = map[x][y] - 'A';
if (x - 1 >= 0 && FM[c].top && FM[map[x - 1][y] - 'A'].bottom && !book[x - 1][y])
dfs(x - 1, y);
if (y - 1 >= 0 && FM[c].left && FM[map[x][y - 1] - 'A'].right && !book[x][y - 1])
dfs(x, y - 1);
if (x + 1 < m && FM[c].bottom && FM[map[x + 1][y] - 'A'].top && !book[x + 1][y])
dfs(x + 1, y);
if (y + 1 < n && FM[c].right && FM[map[x][y + 1] - 'A'].left && !book[x][y + 1])
dfs(x, y + 1);
} int main() {
int sum;
init();
while (cin >> m >> n) {
if (m < 0 || n < 0) break;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
cin >> map[i][j];
memset(book, false, sizeof(book));
sum = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (!book[i][j]) {
++sum;
dfs(i, j);
}
cout << sum << endl;
}
return 0;
}

并查集方法

将二维坐标转化为一维,对每块农田找左、上连通情况,合并等价类

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int F[3100];
char mp[55][55];
int m, n;
char pipe[11][5] = {"1100", "0110", "1001", "0011", "0101",
"1010", "1110", "1101", "1011", "0111", "1111"};
int Find(int x)
{
if(F[x] == -1) return x;
return Find(F[x]);
}
void Union(int x, int y)
{
int t1 = Find(x);
int t2 = Find(y);
if(t1 != t2)
F[t1] = t2;
}
int main()
{
while(scanf("%d%d", &m, &n))
{
if(m<0 || n<0) break;
for(int i = 0; i < m; i++)
scanf("%s", &mp[i]);
memset(F, -1, sizeof(F));
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
{
if(i>0 && pipe[mp[i][j]-'A'][1]=='1' && pipe[mp[i-1][j]-'A'][3]=='1')
Union(i*n+j, (i-1)*n+j);
if(j>0 && pipe[mp[i][j]-'A'][0]=='1' && pipe[mp[i][j-1]-'A'][2]=='1')
Union(i*n+j, i*n+j-1);
}
int cnt = 0;
for(int i = 0; i < m*n; i++)
if(F[i] == -1)
cnt++;
printf("%d\n", cnt);
}
return 0;
}

#1198:Farm Irrigation(DFS + 并查集)的更多相关文章

  1. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. hdu 1198 Farm Irrigation(并查集)

    题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

  3. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  4. HDU 1198 Farm Irrigation (并查集优化,构图)

    本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...

  5. hdu.1198.Farm Irrigation(dfs +放大建图)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  7. hdu1198 Farm Irrigation —— dfs or 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...

  8. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  9. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  10. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Java程序员必备技能:Collections工具类深度解析!

    在之前的文章中,我们学习了单列集合的两大接口及其常用的实现类:在这些接口或实现类中,为我们提供了不少的实用的方法. 本篇文章我们来介绍一种java开发者为我们提供了一个工具类,让我们更好的来使用集合 ...

  2. 解密Prompt系列20. LLM Agent之再谈RAG的召回多样性优化

    几个月前我们就聊过RAG的经典方案解密Prompt系列14. LLM Agent之搜索应用设计.前几天刚看完openAI在DevDay闭门会议上介绍的RAG相关的经验,有些新的感悟,借此机会再梳理下R ...

  3. [NOI online2022提高B] 讨论

    题目描述 有 \(n\) 个人正在打模拟赛,模拟赛有 \(n\) 道题目. 有两人都会的题目并且没有人会的题目包含另一个人时,两者之间才会讨论. (定义第 ii 个人会的题目的集合为 \(S_i\) ...

  4. 华企盾DSC导入离线客户端没有获取到信息

    安装客户端的时候禁用了网卡,导致导入离线客户端获取不到信息. 解决方法: 1.启用网卡,重启电脑 2.启用网卡,重装客户端

  5. 如何实现CesiumJS的视效升级?

    CesiumJS作为一款强大的地理可视化引擎,为我们提供了丰富的地球数据可视化和交互展示的能力.然而,随着用户需求的不断增加和技术的不断进步,如何进一步提升CesiumJS的视觉效果成为了一个重要的问 ...

  6. 如何用python脚本制作生成CANdbc

    最近在工作中,有同事拿了一个excel的dbc表格,在用官方的dbc工具一个一个创建信号,大概看了一下共累计20多个节点,300多个信号,居然在手动处理,顿感无语.. 于是在网络上搜相关的dbc 通过 ...

  7. DNSlog注入(利用DNSlog平台将SQL盲注变成回显注入)

    前言 什么是UNC 什么是DNSlog注入 DNSlog注入的条件 防止DNSlog注入的几个措施 sqli-labs试验 前言 前几天面试的时候,面试官问我知不知道OOB(带外数据). 当时我蒙了, ...

  8. MySQL面试题:一条SQL语句在MySQL中执行过程全解析

    一 .MySQL 基础架构分析 介绍一下下图涉及的一些组件的基本作用帮助大家理解这幅图. 连接/线程处理(连接器): 身份认证和权限相关(如连接处理.授权认证.安全等等). 查询缓存: 执行查询语句的 ...

  9. ZincSearch轻量级全文搜索引擎入门到

    ZincSearch轻量级全文搜索引擎入门到 Zinc是一个用于对文档进行全文搜索的搜索引擎.它是开源的,内置在 Go 中.Zinc不是从头开始构建索引引擎,而是构建在 bluge 之上,这是一个出色 ...

  10. python 处理pdf加密文件

    近期有同事需要提取加密的pdf文件,截取其中的信息,并且重构pdf文件.网上没有搜到相关的pdf操作,于是咨询了chatgpt,给出了pypdf2的使用案例.但是时间比较久远了,很多库内的调用接口都已 ...