题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198

题目:

Farm Irrigation

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

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
 
思路:
这道题很明显就是叫我们求连通块的数量。求连通块无非就是并查集和DFS两种方法,个人认为并查集更适合题目给定两两关系的情况,所以该题我用了DFS。而这道题又和传统的DFS有所不同,涉及到水管的拼接问题。因为四个方向拼接,我们很容易想到用状态压缩来解决。下面我简单地介绍一下状态压缩。
以A为例:
至于我为什么要将二进制状态排成“上左右下”而不是“左右上下”之类的呢。因为水管接通不止要考虑本身,还有下一个管道的方位,两个管道的方位都是相反的。比如J,K,J提供右管道,K提供左管道才能拼接成功。我们设d为J要提供的管道方位,则K提供的管道方位 我们就可以用3-d得出了,这样说有点抽象,看下面的代码会更好理解。
 
代码:
 #include <cstdio>
#include <cstring>
char farm[][];
int mp[]={,,,,,,,,,,};//将A-K状态压缩
int visited[][];
int dirx[]={,,,-};
int diry[]={,,-,};
int n,m;
void dfs(int i,int j){
int x=farm[i][j]-'A';
int vx=mp[x];
for (int d=,xx,yy; d<; d++) {
xx=i+dirx[d];
yy=j+diry[d];
if(xx< || xx>=n || yy< || yy>=m ||visited[xx][yy]) continue;
int t=farm[xx][yy]-'A';
int vt=mp[t];
if((vx>>d)& && (vt>>(-d))&){//连接水管的方向相反
visited[xx][yy]=;
dfs(xx, yy);
}
}
}
int main(){
int res;
while (scanf("%d%d ",&n,&m)!=EOF && n!=- && m!=-) {
res=;
memset(visited, , sizeof(visited));
for (int i=; i<n; i++) gets(farm[i]);
for (int i=; i<n; i++) {
for (int j=; j<m; j++) {
if(visited[i][j]) continue;//visited[i][j]不等于0,则说明它已经属于别的连通块了
visited[i][j]=;
res++;
dfs(i,j);
}
}
printf("%d\n",res);
}
return ;
}

HDU 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) ...

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

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

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

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

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

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

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

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

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

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

  7. hdu 1198 Farm Irrigation

    令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...

  8. hdu 1198 Farm Irrigation(并查集)

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

  9. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 装系统---------- u盘 安装系统

    1.首先根据你的电脑品牌进入bios 模式 如何进入:  不同电脑品牌进入bios 方法 进入之后设置启动方式 将第一启动设置为U盘启动设置好后 使用 Fn + F10  保存 2.使用制作好的 U盘 ...

  2. 个人网站(sysoft.net.cn)被k,公司名都搜索不出来了,怎么办?

    今年上班后,好2019年3月初,上班后(年前大病一场 ,两个月没维护网站),发现公司网站所有收录都掉了,搜索公司名都不不到了,宝宝真是惊呆了.   有些人说是百度出了故障,有人说是百度算法.   说句 ...

  3. Lucene 全文检索入门

    博客地址:http://www.moonxy.com 一.前言 Lucene 是 apache 软件基金会的一个子项目,由 Doug Cutting 开发,是一个开放源代码的全文检索引擎工具包,但它不 ...

  4. 弄懂Java为何只有值传递

    最近有同学问我关于Java中值传递与引用传递的问题,在此小结一下 值传递是指在函数调用时将实参内容复制一份传递给形参,这样在函数中改变该参数不会对原参数产生影响. 引用传递是指将对象地址的引用传递给该 ...

  5. 53 (OC) 皮肤切换

    1:添加皮肤 1.1:iOS皮肤切换方案 http://www.cocoachina.com/ios/20171012/20762.html 1.2:整合 iOS几种换肤思路和demo https:/ ...

  6. [AWS] Lambda by Python

    当前统治数据分析的语言还是Python,还是暂时走:Python + GPU的常规路线好了. numba, pyculib (分装了cublas) Ref: 使用 Python 构建 Lambda 函 ...

  7. 自定义 Alamofire 的 response serializer

    Alamofire 的 DataRequest 类针对二进制数据.字符串.json.属性列表提供了一系列方便解析的方法(内部实际上使用的是 Response Serializer),现在我们要针对服务 ...

  8. Jedis 常用API使用

    使用Jedis操作Redis常用的API <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <depen ...

  9. [LeetCode] 824. Goat Latin

    Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...

  10. Scala 学习笔记之集合(5)

    import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { // ...