题目网址: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. 字节输出流OutputStream

    1.OutputStream是输出字节流的超类. import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...

  2. 超实用的SQL语句之嵌套查询

    嵌套查询 什么是嵌套查询 . 嵌套查询的意思是,一个查询语句(select-from-where)查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询.其中外层查询也称为父查询,主查询. ...

  3. Linux 笔记 - 第十五章 MySQL 常用操作和 phpMyAdmin

    博客地址:http://www.moonxy.com 一.前言 前面几章介绍了 MySQL 的安装和简单的配置,只会这些还不够,作为 Linux 系统管理员,我们还需要掌握一些基本的操作,以满足日常管 ...

  4. charles 视图菜单总结

    本文参考:charles 视图菜单总结 Charles的视图菜单里的东西其实是非常常用的功能: 但是我们一般是不需要从这里点进来的: 里面,无非是查看的视图结构(按照域名和按照访问时间) 然后是一些概 ...

  5. java架构之路-(spring源码篇)由浅入深-spring实战详细使用

    今天我更新了一篇jvm垃圾回收的算法和垃圾回收器的内部逻辑,但是看的人不多啊......貌似大家还是比较喜欢看源码吧,毕竟实战要比理论用的多. 这篇文章不会详细的深入底层源码,只是基于注解和配置来说说 ...

  6. Git同步更新操作GitHub和码云仓库上面的代码

    一.前言 问题: 小编在生活中,一般都是将代码保存到github上,但由于国内的码云仓库确实速度比github快很多,用起来也很方便,于是后来就慢慢转码云了,当然小编在github上的代码也不想放弃更 ...

  7. Linux遇到的问题-记录

    Linux遇到的问题 2019-04-09以前: Linux&Win双系统下时间显示不正常的问题 一般安装了双系统(Linux+Windows)就很容易出现问题,Windows是直接取硬件时间 ...

  8. 性能测试:Jmeter-Beanshell请求加密实例

    进行性能测试时,有可能遇到一种场景:接口请求由于安全问题,需要进行加密发送. 这种场景下,使用Jmeter实现性能测试,则也需要使用同样的加密规则发送请求报文. 要实现此类性能测试有几种策略: 直接去 ...

  9. append追加的html片段,添加点击事件没有反应,解决!

    对于追加的元素来说,属于未来展现的内容,我们此时不能使用一般点击事情处理,需要用未来事件绑定. 例如:我点击车牌号显示车辆详情,如下绑定. //绑定未来元素 $("body").d ...

  10. laravel基础操作手册

    laravel基础操作手册 1.路由配置 测试配置路由: Route::get('/test', 'TestController@index'); 2.控制器书写 3.模型文件 4.增加扩展类文件 L ...