ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源
DFS的连通块问题 两个相邻农田的管道能够直接连接的话他们就属于一个连通块 题目就是求连通块个数
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 55;
char mat[N][N];
int type[11][4] = { //相应11种水管类型 按顺时针方向有管道的为1否则为0
{1, 0, 0, 1}, {1, 1, 0, 0}, {0, 0, 1, 1}, {0, 1, 1, 0},
{1, 0, 1, 0}, {0, 1, 0, 1}, {1, 1, 0, 1}, {1, 0, 1, 1},
{0, 1, 1, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}
}; int dfs(int r, int c)
{
int cur = mat[r][c] - 'A';
if(cur < 0 || cur > 10) return 0;
mat[r][c] = 0; //标记已经訪问 0-'A'是小于0的
int up = mat[r - 1][c] - 'A', dw = mat[r + 1][c] - 'A',
le = mat[r][c - 1] - 'A', ri = mat[r][c + 1] - 'A'; //4个相邻块的管道类型
if(up > -1 && up < 11 && type[up][2] && type[cur][0]) dfs(r - 1, c);
if(dw > -1 && dw < 11 && type[dw][0] && type[cur][2]) dfs(r + 1, c);
if(le > -1 && le < 11 && type[le][1] && type[cur][3]) dfs(r, c - 1);
if(ri > -1 && ri < 11 && type[ri][3] && type[cur][1]) dfs(r, c + 1);
return 1; //一个连通块中仅仅有一个返回1
} int main()
{
int n, m, ans;
while (scanf("%d%d", &n, &m), n >= 0)
{
ans = 0;
memset(mat, 0, sizeof(mat));
for(int i = 1; i <= n; ++i)
scanf("%s", mat[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
ans += dfs(i, j);
printf("%d\n", ans);
}
return 0;
}
Farm Irrigation
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
name=0000%2F2412%2F1.gif" alt="">
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
name=0000%2F2412%2F2.gif" alt="">
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
版权声明:本文博客原创文章,博客,未经同意,不得转载。
ZOJ 2412 Farm Irrigation(DFS 条件通讯块)的更多相关文章
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 2412 Farm Irrigation
题目: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- hdu1198 Farm Irrigation —— dfs or 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...
- 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 (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- ZOJ2412 Farm Irrigation(农田灌溉) 搜索
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- HDUOJ--------(1198)Farm Irrigation
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- zabbix监控nginx连接状态(转)
zabbix监控nginx zabbix可以监控nginx的状态,关于一个服务的状态可以查看服务本身的状态(版本号.是否开启),还应该关注服务能力(例如以nginx的负载效果:连接数.请求数和句柄数) ...
- 开启本地MySql数据库远程连接
解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 33 ...
- GEF的MVC体系结构
摘要: 本文首先介绍了标准的 MVC 体系构架,同时也介绍了最常见的一类 MVC 模式的变种.之后,文章重点介绍了 MVC 结构在 gef 框架中的体现与应用,以及 gef 是如何综合利用工厂模式.命 ...
- 存储管理(一):openfiler介绍及存储理解
openfiler是一个免费的.开源的基于浏览器的网络存储产品,支持基于文件的的网络连接存储(NAS)和基于块的存储区域网(SAN).支持的协议有smb,cifs,nfs,http/dev和ftp. ...
- Python3.2官方文档-日志和弱引用
8.5 日志 Logging模块提供了一些功能全面和灵活的日志系统.最简单的形式就是把日志信息发送到一个文件或sys.stderr; import logging logging.debug('Deb ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...
- android EditText的美化
今天要做一个页面,有EditText,于是就搞起了它的美化. EditText的美化,我的第一反应是,在EditText的左边设置显示一张图片,这样会比較好看. 设置左边显示图片的属性为:androi ...
- 观察者模式(Observer Patterns)
今天学习了观察者模式,做个总结,方便以后回想. 首先是定义:观察者模式就是定义对象之间一对多的依赖关系,当一个对象状态发生改变时,全部依赖他的对象都收到推送消息并自己主动更新做出改变. 我的理解:生活 ...
- JDK源码学习系列04----ArrayList
JDK源码学习系列04----ArrayList 1. ...
- JAVA网络编程Socket常见问题 【长连接专题】
一. 网络程序运行过程中的常见异常及处理 第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发生在服务器端进行new ...