HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址: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
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.

#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)的更多相关文章
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation (并查集优化,构图)
本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...
- hdu 1198 Farm Irrigation
令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 装系统---------- u盘 安装系统
1.首先根据你的电脑品牌进入bios 模式 如何进入: 不同电脑品牌进入bios 方法 进入之后设置启动方式 将第一启动设置为U盘启动设置好后 使用 Fn + F10 保存 2.使用制作好的 U盘 ...
- 个人网站(sysoft.net.cn)被k,公司名都搜索不出来了,怎么办?
今年上班后,好2019年3月初,上班后(年前大病一场 ,两个月没维护网站),发现公司网站所有收录都掉了,搜索公司名都不不到了,宝宝真是惊呆了. 有些人说是百度出了故障,有人说是百度算法. 说句 ...
- Lucene 全文检索入门
博客地址:http://www.moonxy.com 一.前言 Lucene 是 apache 软件基金会的一个子项目,由 Doug Cutting 开发,是一个开放源代码的全文检索引擎工具包,但它不 ...
- 弄懂Java为何只有值传递
最近有同学问我关于Java中值传递与引用传递的问题,在此小结一下 值传递是指在函数调用时将实参内容复制一份传递给形参,这样在函数中改变该参数不会对原参数产生影响. 引用传递是指将对象地址的引用传递给该 ...
- 53 (OC) 皮肤切换
1:添加皮肤 1.1:iOS皮肤切换方案 http://www.cocoachina.com/ios/20171012/20762.html 1.2:整合 iOS几种换肤思路和demo https:/ ...
- [AWS] Lambda by Python
当前统治数据分析的语言还是Python,还是暂时走:Python + GPU的常规路线好了. numba, pyculib (分装了cublas) Ref: 使用 Python 构建 Lambda 函 ...
- 自定义 Alamofire 的 response serializer
Alamofire 的 DataRequest 类针对二进制数据.字符串.json.属性列表提供了一系列方便解析的方法(内部实际上使用的是 Response Serializer),现在我们要针对服务 ...
- Jedis 常用API使用
使用Jedis操作Redis常用的API <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <depen ...
- [LeetCode] 824. Goat Latin
Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...
- Scala 学习笔记之集合(5)
import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { // ...