题目网址: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. Java第二次作业第三题

    四叶玫瑰线的图形设计:当用鼠标拖拽改变窗口大小时,四叶玫瑰线会重新绘制 package naizi; import java.awt.*; import java.awt.event.*; impor ...

  2. DirectX12 3D 游戏开发与实战第四章内容(下)

    Direct3D的初始化(下) 学习目标 了解Direct3D在3D编程中相对于硬件所扮演的角色 理解组件对象模型COM在Direct3D中的作用 掌握基础的图像学概念,例如2D图像的存储方式,页面翻 ...

  3. 基于 HTML5 WebGL 的医疗物流系统

    前言 物联网( IoT ),简单的理解就是物体之间通过互联网进行链接.世界上的万事万物,都可以通过数据的改变进行智能化管理.ioT 的兴起在医疗行业中具有拯救生命的潜在作用.不断的收集用户信息并且实时 ...

  4. Android四大组件之服务的两种启动方式详解

    Service简单概述 Service(服务):是一个没有用户界面.可以在后台长期运行且可以执行操作的应用组件.服务可由其他应用组件启动(如:Activity.另一个service).此外,组件可以绑 ...

  5. Widget 基础

    一切皆Widget Widget 渲染过程 Flutter把视图数据的组织和渲染抽象为三部分,即 Widget.Element 和 RenderObject. Widget Widget 是空间实现的 ...

  6. Linux 文件复制命令cp

    文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...

  7. rpm,yum

    rpm RedHat Package Manager软件包管理器的核心功能:1.制作软件包2.安装.卸载.升级.查询.校验.数据库的重建.验证数据包等工作 安装: rpm -i    /PATH/TO ...

  8. Salesforce学习之路-developer篇(三)利用Visualforce Page实现页面的动态刷新案例学习

    Visualforce是一个Web开发框架,允许开发人员构建可以在Lightning平台上本地托管的自定义用户界面.其框架包含:前端的界面设计,使用的类似于HTML的标记语言:以及后端的控制器,使用类 ...

  9. Map(映射)

    散列表介绍: 数组和链表都可以是有序的(即存储顺序与取出顺序一致),但这样是有代价的,需要遍历才可以寻找某一特定元素: 而还有另外的一些存储结构:不在意元素的顺序,能够快速的查找元素的数据 其中就有一 ...

  10. 点集配准技术(ICP、RPM、KC、CPD)

    在计算机视觉和模式识别中,点集配准技术是查找将两个点集对齐的空间变换过程.寻找这种变换的目的主要包括:1.将多个数据集合并为一个全局统一的模型:2.将未知的数据集映射到已知的数据集上以识别其特征或估计 ...