题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1198

题目大意:

有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。

例如下面这个土地块

ADC

FJK
IHE

then the water pipes are distributed like

解题思路:

首先模拟A-K的四个不同的方向,用了一个数组表示从A-K的四个方向是否有水管

然后枚举每个点的四个方向上可到达的地方,如果连通就合并并查集,最后根节点的数目就是连通块的数目(DFS也可以做,判断连通块即可)

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
int p[maxn];
char Map[][];
int dir[][] = {,-,-,,,,,};//分别是左(0),上(1),右(2),下(3)
int G[][]={
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
};
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
}
void Union(int x, int y)
{
x = Find(x);
y = Find(y);
if(x != y)
{
p[x] = y;
}
}
int main()
{
int n, m;
freopen("out.txt", "w", stdout);
while(cin >> n >> m && ( n + m > ))
{
init();
for(int i = ; i < n; i++)
{
cin >> Map[i];
}
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
int c = Map[i][j] - 'A';
for(int k = ; k < ; k++)//枚举四个方向
{
if(!G[c][k])continue;//判断该方向是否存在水管
int x = i + dir[k][];
int y = j + dir[k][];
if(x < || x >= n || y < || y >= m)continue;
int d = Map[x][y] - 'A';
if(!G[d][(k + ) % ])continue;//这里是判断(x,y)处是否有和k方向相对的方向的水管
Union(i * m + j, x * m + y);//这里是编号i*m+j,一开始写错成i*n+j
}
}
}
int flag = ;
for(int i = ; i < n * m; i++)
{
if(p[i] == i)flag++;//根节点的数目也就是连通块的数目
}
cout<<flag<<endl;
}
return ;
}

测试数据:

 JBHFFKBAHHFIGHDHJ
CHHIKGHIFGHCBJKKC
EJHFEHFGHAFAFEFBD
KGAAGHIHKDFCKJKDK
BB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JKCIEKACKJDJCAABGEAFJDKAAGC
DDKKJHDDDGJIADDDGEKEEJAJDEGDD KAFKGFKCFABFGCHBGEAHBCBBDHKBBDHIJEJIIACHDCBICDAH
AHIEDHBIEGIIKBAEHJJFJDBGBIKKHBAFDADCAIEEJFGCAKEK
KCDDJCDEBGHGDCFFKIIEBEJHDDAHCIDGADGHAAIFDHACJCFH
DFJHDJCEAGGEAICHEHGDKFCIGKDKEFCFFFIJKHGGECGIKKKA
AJACFFJJFCDGDIGAGCHJGKAHGEFIECACKIBGAAIBHDHIEGAF
GJIBDFIBHAGAFIDGKFGHCJAJICDJIBJEAJDAEBKGCFIJDEGC
HBABAJCHGFKGIBGBIEJDEHJAKFFHFBBHHHAIJIEDGKKBDKIJ
KIKJDKBIGCJAAIJHDAGFBEEKEFKGDHJDABCIGGBKDEFJDCKB
FDFJADGHAFFEFKIDKJBGIEHDCDGKJBICHCIAKDGBAKGCFKDI
IHJHAKDHGEJHHGGFEIHIFBEGHAJKGAEJDCAKHJKABJKGBBKB
GHBAHCIFFJGFBCACABIDFKGHHDJDKGEEIAIKKEGFHGFEBBCD
HIBADGAAHJAIAGGKCDHIBAAEKKCAHGEDDKEHHFFJGIJAHICE
CFGCAIBCDDECDJHKCJGJDKCBHDAHIKDJKAJJDADCFEKJKAFA
GCDEFHEJAIAJDGFDDADJGEIAJCKBICDGABKEDAFFGBIIHDBE
AIFDACKBJHBEEGIJDFBCJEKFBHICGGBFJCHIBJCIJIJCHAEF
KBEFKDCIEEDEBKIIBGDHGJFHGKCHJGCJFIGIDACBIGCJHKEK
CIICHEEIBGHHFCICKGECHJFBFKHBAKEGBCKCHKHKCKFDHJDG
GIKABBAIKJJDEECKBEECEHBHECDEGDICCGAGCCHCJAADBIFK

 

答案:


                          

hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)的更多相关文章

  1. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  2. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  3. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu 1198 Farm Irrigation(并查集)

    题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

  5. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  6. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  7. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. hdu1198 Farm Irrigation 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...

  9. hdu.1198.Farm Irrigation(dfs +放大建图)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. hdu 1198 Farm Irrigation

    令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...

随机推荐

  1. Java 异常处理基本规则,Java异常处理的基本规范

    看了团队中原来代码中的异常处理,心碎了一地,稍微对照阿里巴巴的异常处理规范整理了一遍,准备分享一下,Java的异常处理规范&约束. 一.运行异常的扑捉 不要捕获 Java 类库中定义的继承自  ...

  2. UVA307 Sticks

    题意:一组等长的木棒,将它们随机的砍掉,得到若干根小木棍, 每一节小棍的长度都不超过50个单位.然后想把这些木棍拼接起来,恢复到裁剪前的状态, 但忘记了初始时有多少木棒以及木棒的初始长度.计算木棒的可 ...

  3. 需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数 及 多属性节点查询

    第一个问题, 1. 首先需要引入命名空间, 2. 其次,在用xpath查找结点时,在selectNodes等方法中再次带入命名空间 如 XmlDocument doc = new XmlDocumen ...

  4. redmine迁移

    (redmine使用的是bitnamiredmine一键安装,环境为apache+mysql+php) 在新环境中一键安装bitnamiredmine,安装完后,执行下面操作 1.备份原环境数据库,恢 ...

  5. 将GPT转换成MBR

    准备一个pe启动盘 1.单击”运行“在弹出来的窗口输入cmd回车 2.在输入“diskpart”回车 3.在输入“list disk”显示硬盘信息,查看那个盘是gpt分区类型 4.输入“select ...

  6. UiAutomator环境配置

    前提:下载jdk以及adt 一.配置java开发环境 1.新建系统变量 -——变量名:JAVA_HOME      变量值:C:\Program Files\Java\jdk1.7.0_80 2.编辑 ...

  7. 大数据“重磅炸弹”——实时计算框架 Flink

    Flink 学习 项目地址:https://github.com/zhisheng17/flink-learning/ 博客:http://www.54tianzhisheng.cn/tags/Fli ...

  8. linux下mysql-5.5.27.tar.gz源程序包安装实例

    研究了好几天,终于把mysql装上了,现在来做下小结. 系统环境:fedora8 虚拟机. 1.检查安装使用的编译工具gcc是否存在,如果不存在则要下载安装 # gcc -v 2.卸载低版本的mysq ...

  9. Java中的==和equals区别

    概述: A.==可用于基本类型和引用类型:当用于基本类型时候,是比较值是否相同:当用于引用类型的时候,是比较对象是否相同. B.对于String a = “a”; Integer b = 1;这种类型 ...

  10. 北航oo作业第三单元小结

    一.梳理JML语言的理论基础 1.jml的注释结构 jml注释语言的每一行都以@作为开始,若是块注释,则需要在注释块的首尾使用/*@ 与@*/ 2.jml的表达式体系 1.原子表达式 表达式可以看作是 ...