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种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- react-native android 打包发布
react-native android 打包步骤 <一>.生成签名文件(应用身份证) 1.使用keytool命令 keytool -genkey -v -keystore my-r ...
- IDEA 2018.2破解
最新的IDEA激活方式 使用网上传统的那种输入网址的方式激活不了,使用http://idea.lanyus.com/这个网站提供的工具进行 1.进入hosts文件中:C:\Windows\System ...
- iOS开发之CoreData数据存储
iOS开发之CoreData数据存储 参考资料:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreD ...
- c++ 跳转语句块
p170~p172:跳转语句:1.break:对while for switcho有效!2.continue:中断当前迭代,但是循环还要继续.因此对while for有效,对switch无效!3.go ...
- SQL学习笔记之MySQL索引知识点
0x00 概述 之前写过一篇Mysql B+树学习,简单的介绍了B+数以及MySql使用B+树的原因, 有了这些基础知识点,对MySql索引的类型以及索引使用的一些技巧,就比较容易理解了. 0x01 ...
- git推送到github报错:error: The requested URL returned error: 403 Forbidden while accessing https://github.com
最近使用git命令从github克隆仓库到版本,然后进行提交到github时报错如下: [root@node1 git_test]# git push origin mastererror: The ...
- 《JAVA程序设计》第五次实验报告
20145333 实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21:3 ...
- LeetCode——Longest Palindromic Subsequence
1. Question Given a string s, find the longest palindromic subsequence's length in s. You may assume ...
- MongoDB 性能优化
Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...
- swoole http_server 多进程并使用多进程处理消息
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set([ 'worker_num' =&g ...