HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 38 Accepted Submission(s) : 24
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Output
Sample Input
2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1
Sample Output
2
3
Author
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
int mp[]={,,,,,,,,,,};
//表示一种状态,B(1<<0)+(1<<1)=3:表示上右是通的.
int dr[][]={{-,},{,},{,},{,-} };// 上,右,下,左
int n,m;
char ch[][];
int fa[*];
bool ok(int x,int y)
{
if (x< || x>=n) return ;
if (y< || y>=m) return ;
return ;
}
int findfa(int k)
{
if (fa[k]==k) return k;
else return fa[k]=findfa(fa[k]);
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
if (n< || m<) break;
for(int i=;i<n;i++)
scanf("%s",&ch[i]); for(int i=;i<n*m;i++) fa[i]=i; for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
for(int k=;k<;k++) //向右,向下两个方向
{
int x=i+dr[k][];
int y=j+dr[k][];
if (!ok(x,y)) continue;
if ( (mp[ch[i][j]-'A']&(<<k))== || (mp[ch[x][y]-'A']&(<<((k+)%)))== ) continue;
int fx=findfa(i*m+j); //之前一直错,问题在这里应该是*m而不是n。
int fy=findfa(x*m+y);
if (fx!=fy) fa[fx]=fy;
} } int sum=;
for(int i=;i<n*m;i++) if (fa[i]==i) sum++;
printf("%d\n",sum);
}
return ;
}
HDU 1198 Farm Irrigation(并查集+位运算)的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- 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(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...
- 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
令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- BZOJ 2301 Problem b (莫比乌斯反演+容斥)
这道题和 HDU-1695不同的是,a,c不一定是1了.还是莫比乌斯的套路,加上容斥求结果. 设\(F(n,m,k)\)为满足\(gcd(i,j)=k(1\leq i\leq n,1\leq j\le ...
- springer论文模板参考文献的顺序问题
latex环境 MikTex 2.9 + TeXstudio 2.12.8 (+ Mendeley) 问题 springer提供的latex模板 中最后的参考文献是按照字母顺序排列的.我想要弄成按照文 ...
- tomcat源码调试
三.tomcat目录结构 tomcat的下载安装有很多教程,不再赘述. 现在的tomcat已经到9了,当tomcat下载安装完成后,其目录大致如下: 除了上面的文件夹,还有四个文件: ...
- [转] 把eclipse设置为黑色主题 方式二
首先,废话不多说,给大家看一下我设置成黑色主题后的效果: 至于怎么达到这个效果呢,首先是中间的编辑区. 从我的云盘里下载压缩包,解压到eclipse目录的dropins文件夹下,你就会有各种各样的编辑 ...
- GridView自定义自增长的 序号 列
如图所示,添加一个普通列(非模板列),将其显示文本为 序号 在GridView的RowDataBound事件中作如下处理 后台.CS 代码:
- 重新想,重新看——CSS3变形,过渡与动画③
这一篇主要谈谈CSS3的过渡属性. 过渡属性被设计的十分通俗易懂,属性写法为transition,有四个子属性: <transition-property> 表示需要过渡的属性[必须](本 ...
- 钓鱼WIFI搭建
1.无线网卡 2.KaliLinux操作系统,这里就不用说了,必备的 3.isc-dhcp-server服务器.安装好KaliLinux后只需要apt-get update 然后apt-get i ...
- ubuntu18.04编译openwrt前的准备
1.获取openwrt源码 git clone https://github.com/openwrt/openwrt.git 2.安装一些库及必备程序: sudo apt-get install li ...
- [BZOJ1131]Sta
Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...
- caffe2+cuda+Ubuntu16.04(u盘安装)
安装caffe2 预先准备.安装gflags及autoconf及GLOG https://github.com/caffe2/caffe2/issues/1810 一.下载源代码通过网盘 https: ...