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 ...
随机推荐
- Unity3D-游戏场景优化之遮挡剔除(Occlusion Culling)的使用
在大型3D游戏场景中,如何优化游戏性能是非常重要的一步.一般遮挡剔除是非常常用的.接下来我们看看如何使用遮挡剔除. 假设这是一个游戏场景. 下面这是相机的视口,相机的视觉是看不到很大立方体后面的那些小 ...
- Spring入门(十四):Spring MVC控制器的2种测试方法
作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...
- 云原生生态周报 Vol. 21 | Traefik 2.0 正式发布
作者 | 浔鸣.心水.元毅.源三.衷源 业界要闻 CNCF 计划将 TOC 升至 11 人 技术监督委员会(TOC)是 CNCF 的三大核心管理机构之一,从 2020 年 1 月起,TOC 将从 9 ...
- 【求赐教】VMware workstation 转VSphere
首先我从其他电脑拷贝过来一台虚拟机(这个说法不知道准不准确,就是把所有文件夹都拷贝过来了),然后打开VMware,通过"打开虚拟机"这个操作,直接找到本地的.vmx文件,如下图所示 ...
- 教老婆学Linux运维(二)Linux常用命令指南【下】
目录 tips:紧接上一篇,Linux常用命令指南[上] 2.4 文件解压缩 2.4.1 官宣的linux压缩工具:tar tar的基本命令格式为 tar [参数选项] [文件或目录] 我们前面学的命 ...
- SpringBoot导入jsp依赖始终报错
先粘出我自己的pom代码: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 并发编程的模型分类(转载于https://link.zhihu.com/?target=http%3A//www.54tianzhisheng.cn/2018/02/28/Java-Memory-Model/)强烈推荐!
在并发编程需要处理的两个关键问题是:线程之间如何通信 和 线程之间如何同步. 通信 通信 是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信机制有两种:共享内存 和 消息传递. 在共享内 ...
- Kafka0.11之RoundRobinPartitioner/HashPartitioner(Scala):
RoundRobinPartitioner/HashPartitioner: import java.util import java.util.concurrent.atomic.AtomicLon ...
- MySql自定义函数-关于保留小数位的特殊需求
背景 昨天,关于价格详情接口又来了一个小需求,而且有点特别.价格显示:改为保留两位小数,没错,就是保留两位小数.大家是不是想说这没啥特别的...数据库都有函数搞定了.例如四舍五入的ROUND(x,d) ...
- (转) websocket 和 socket 剖析
Socket 与 WebSocket 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本站部分原创和翻译文 ...