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种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- List泛型集合转DataTable
自存,此方法可以防止出现DataSet不支持System.Nullable的错误 public static DataTable ToDataTable<T>(IEnumerable< ...
- 20145316《Java程序设计》实验一:Java开发环境的熟悉(Windows + IDEA)
20145316<Java程序设计>实验一:Java开发环境的熟悉(Windows + IDEA) 一.DOC命令行下Java程序开发 1.打开cmd,输入 mkdir 20145316命 ...
- mongodb研究(mongodb 内存数据库)
本日志大部分都不是原创的转载复制的会带链接保持版权 工作中使用mongodb已经好久了,讽刺的是到了最后快离职的时候才有时间好好研究下源码. 印象:mongodb是一个内存数据库,数据都是放到内存 ...
- 【分库分表】sharding-jdbc—分片策略
一.分片策略 Sharding-JDBC认为对于分片策略存有两种维度: 数据源分片策略(DatabaseShardingStrategy):数据被分配的目标数据源 表分片策略(TableShardin ...
- slf4j和log4j、logback
现在主流java项目一般使用slf4j+log4j的日志方案,最近抽点时间扫了一下slf4j.log4j.logback的官方文档,做个笔记.这篇比较不打算描述具体的配置方法,因为官方文档已经讲得很清 ...
- LeetCode——Counting Bits
Question Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calcu ...
- jar包中使用log4j2不起作用
某程序中有使用到log4j2,将该程序打包成jar,使用以下命令执行时,发现log4j不输出 java -cp Tool.jar com.zhen.nameOnce.Log4jTest 且报以下错误 ...
- Vjudge - B - 这是测试你会不会排序
2017-07-14 22:44:11 writer:pprp 题目: 他们提出的比赛规则是这样的: 1. 每方派出n个人参赛: 2. 出赛的顺序必须是从弱到强(主要担心中国人擅长的田忌赛马): ...
- [小问题笔记(五)] 用SQL加密字符串(MD5、SHA1),顺便解决读取数据加密后不一样的问题
这里用到SQL Server内置的函数 HashBytes(). select HashBytes('MD5','bubu') select HashBytes('SHA1','bubu') 以MD5 ...
- JSch 实现 SSH 端口转发
package com.yinger.webservice.test; import java.sql.Connection; import java.sql.DriverManager; impor ...