hdu1198Farm Irrigation (DFS)

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.
DK
HF
3 3
ADC
FJK
IHE
-1 -1
3
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct nn
{
int d[4];//按顺序左,上,右,下;0表示不路通,1表示路通
}node;
node map[55][55],N[11];
int n,m,vist[55][55],dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
void set_N()
{
for(int i=0;i<11;i++)
{
if(i==0){N[i].d[0]=N[i].d[1]=1;N[i].d[2]=N[i].d[3]=0;}
if(i==1){N[i].d[1]=N[i].d[2]=1;N[i].d[0]=N[i].d[3]=0;}
if(i==2){N[i].d[0]=N[i].d[3]=1;N[i].d[2]=N[i].d[1]=0;}
if(i==3){N[i].d[2]=N[i].d[3]=1;N[i].d[1]=N[i].d[0]=0;}
if(i==4){N[i].d[1]=N[i].d[3]=1;N[i].d[2]=N[i].d[0]=0;}
if(i==5){N[i].d[0]=N[i].d[2]=1;N[i].d[1]=N[i].d[3]=0;}
if(i==6){N[i].d[0]=N[i].d[1]=N[i].d[2]=1;N[i].d[3]=0;}
if(i==7){N[i].d[0]=N[i].d[1]=N[i].d[3]=1;N[i].d[2]=0;}
if(i==8){N[i].d[0]=N[i].d[2]=N[i].d[3]=1;N[i].d[1]=0;}
if(i==9){N[i].d[1]=N[i].d[3]=N[i].d[2]=1;N[i].d[0]=0;}
if(i==10){N[i].d[0]=N[i].d[3]=N[i].d[2]=N[i].d[1]=1;}
}
}
void dfs(int x,int y)
{
int tx,ty;
vist[x][y]=1;
for(int e=0;e<4;e++)
if(map[x][y].d[e])
{
tx=x+dir[e][0];ty=y+dir[e][1];
if(!vist[tx][ty]&&tx>=0&&tx<n&&ty>=0&&ty<m)
{
if(e==0&&map[tx][ty].d[2]||e==1&&map[tx][ty].d[3])
dfs(tx,ty);
if(e==2&&map[tx][ty].d[0]||e==3&&map[tx][ty].d[1])
dfs(tx,ty);
}
}
}
int main()
{
char c;
int k;
set_N();
while(scanf("%d%d",&n,&m)>0&&n+m!=-2)
{
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
cin>>c;
map[i][j]=N[c-'A'];
vist[i][j]=0;
}
} k=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(!vist[i][j])
{
k++;
dfs(i,j);
}
printf("%d\n",k);
}
}
hdu1198Farm Irrigation (DFS)的更多相关文章
- hdu1198Farm Irrigation(dfs找联通)
题目链接: 啊哈哈,选我选我 思路是:首先依据图像抽象出联通关系.. 首先确定每一种图形的联通关系.用01值表示不连通与不连通... 然后从第1个图形进行dfs搜索.假设碰到两快田地能够联通的话那么标 ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- 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) ...
- 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)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- (DFS)hdoj1198-Farm Irrigation
题目链接 DFS的简单应用,比较繁琐的是处理输入的英文字母.用并查集也可以做(可是笔者现在还没有掌握并查集,之前只用过一次,以后学会回来补上) #include<cstdio> #incl ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
随机推荐
- jz2440: linux/arch/arm/下面的plat-和mach-
jz2440: linux/arch/arm/下面的plat和mach plat-s3c24xxmach-s3c2440mach-s3c2410 ====================== 1. 三 ...
- C++中(int&)和(int)的区别
在说这个问题之前,先说两个需要知道的背景知识: (1)语言的类型的强制转换不会修改原来的数据,会另外的开辟一个临时的或者程序中指定的空间来存储强制转换后的值. (2)C++引用的实现是在符号表中动了手 ...
- 转:requirejs:让人迷惑的路径解析(~~不错)
接触过requirejs的童鞋可能都知道,无论是通过define来定义模块,还是通过require来加载模块,模块依赖声明都是很重要的一步.而其中涉及到的模块路径解析,对于新手来说,有的时候会让人觉得 ...
- session 共享
目前大多数大型网站的服务器都采用了分布式的部署方式,但是session是在服务器端保存的,如果用户跳转到其他服务器的话,session就会丢失,于是就有了分布式系统的session共享问题. sess ...
- cocos2d-x游戏开发系列教程-超级玛丽04-AppDelegate
代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 背景 上一篇博文提到在CCApplication: ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- poj 3691
ac自动机+dp 自动机上的节点来作为状态 dp[i][j]表示长度为i状态为j至少需要转换多少个字符 #include <iostream> #include <cstdio> ...
- Cocos2d-x 精灵碰撞检測(方法二)
将"Cocos2d-x 精灵碰撞检測(方法一)" update函数改动一下. 使用精灵boundingBox函数获取直接精灵边界框, 不用自己计算精灵矩形大小了,还比較精确,然后调 ...
- F - Free DIY Tour(动态规划,搜索也行)
这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...
- c++隐藏实例
隐藏:是指派生类的函数屏蔽了与其同名的基类函数,规则如下:(1)如果派生类的函数与基类的函数同名,但是参数不同.此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆). 很简单略去 ...