Farm Irrigation ZOJ 2412(DFS连通图)
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
题目意思:有11种正方形农田,每种正方形农田里面对应一种形状的水管,不同的的正方形一用A到K表示,给一个矩阵,问至少需要多少个水源可以使矩形中所有的地方都可以被灌溉,如果两个相邻的正方形的
水管正好对口,那么这两个正方形可以共用一个水源。 解题思路:明显的DFS求连通区域,不过难点在于对农田水管的处理,我们可以开一个结构体数组,里面存放每一块农田四方向的水管,1代表有,0代表无。在搜索的过程中,假如当前的农田有向上的水管,而当前
农田的上面一块农田恰好有向下的水管,那么就可以从当前的水管向上搜索了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int maps[][];
int vis[][];
struct node
{
int up;
int down;
int left;
int right;
};
node num[]= {{,,,},{,,,},{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},{,,,}};
void DFS(int x,int y)
{
int i;
int a,b;
if(x<=||x>m||y<=||y>n||vis[x][y])
{
return ;
}
vis[x][y]=;
for(i=; i<; i++)
{
if(i==)///向上走
{
a=x-;
b=y+;
if(num[maps[x][y]].up&&num[maps[a][b]].down)
{
DFS(a,b);
}
}
else if(i==)///向下走
{
a=x+;
b=y+;
if(num[maps[x][y]].down&&num[maps[a][b]].up)
{
DFS(a,b);
}
}
else if(i==)///向左走
{
a=x+;
b=y-;
if(num[maps[x][y]].left&&num[maps[a][b]].right)
{
DFS(a,b);
}
}
else if(i==)///向右走
{
a=x+;
b=y+;
if(num[maps[x][y]].right&&num[maps[a][b]].left)
{
DFS(a,b);
}
}
}
return ;
}
int main()
{
char ch;
int i,j;
int counts;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,,sizeof(vis));
if(m==-&&n==-)
{
break;
}
getchar();
counts=;
for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
scanf("%c",&ch);
maps[i][j]=ch-'A';
}
getchar();
} for(i=; i<=m; i++)
{
for(j=; j<=n; j++)
{
if(!vis[i][j])
{
counts++;
DFS(i,j);
}
}
}
printf("%d\n",counts);
}
return ;
}
Farm Irrigation ZOJ 2412(DFS连通图)的更多相关文章
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 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)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两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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) ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
随机推荐
- 什么是设计模式?【php】
原文地址:https://www.cnblogs.com/zhuiluoyu/p/5818974.html 什么是设计模式? 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经 ...
- ionic ios 打包 真机测试常见问题
1.ionic 项目在windows下正常打包安卓包时 迁移到mac下打包ios时 不需要复制平台目录platforms即可 不用再mac下去安装各种插件信息 2.ionic 下不能访问api信 ...
- 将select的默认小三角替换成别的图片,且实现点击图片出现下拉框选择option
最近做项目,要求修改select下拉框的默认三角样式,因为它在不同浏览器的样式不同且有点丑,找找网上也没什么详细修改方法,我就总结一下自己的吧. 目标是做成下图效果: 图一:将默认小三角换成红圈的三角 ...
- 对大数据的批量导入MySQL数据库
自己的库里有索引在用insert导入数据时会变慢很多 使用事务+批量导入 可以配置使用spring+mybatis整合的方式关闭自动提交事务(地址),选择批量导入每一百条导入使用list存储值传入到m ...
- 树莓派3B+学习笔记:4、查看GPIO
GPIO(General Purpose I/O Ports)意思为通用输入/输出端口. 可以在终端重直接查看GPIO的定义. 查看方式1: gpio readall 查看方式2: pinout 可以 ...
- Home Assistant系列--之树莓派安装Samba 和 Jupyter Notebook
1.什么是Samba? Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上 ...
- python 爬虫基础知识(继续补充)
学了这么久爬虫,今天整理一下相关知识点,还会继续更新 HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法 ...
- 【转载++】fopen返回0(空指针NULL)且GetLastError是0
结论来看,是一个简单又朴素的道理——打开文件句柄用完了得给关上.表现在现象上却是着实让人费解,以至于有人还怀疑起了微软的Winodws系统来了,可笑至极.还是那句话,先把自己的屁股先给擦干净喽再怀疑别 ...
- HIVE-分桶表的详解和创建实例
我们学习一下分桶表,其实分区和分桶这两个概念对于初学者来说是比较难理解的.但对于理解了的人来说,发现又是如此简单. 我们先建立一个分桶表,并尝试直接上传一个数据 buckets row format ...
- 微信小程序解决地图上的层级关系
在有带地图的手机页面上,view无法显示在地图上方,所以,在wxml中,使用: <cover-view></cover-view> 能使view显示在地图上 注: 在该标签内部 ...