hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)
题目链接:
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
EJHFEHFGH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HGDJAKCHHEADHEJAJHABDCDJHBJI
EHJKCIEKACKJDJCAABGEAFJDKAAGC
DDKKJHDDDGJIADDDGEKEEJAJDEGDD KAFKGFKCFABFGCHBGEAHBCBBDHKBBDHIJEJIIACHDCBICDAH
AHIEDHBIEGIIKBAEHJJFJDBGBIKKHBAFDADCAIEEJFGCAKEK
KCDDJCDEBGHGDCFFKIIEBEJHDDAHCIDGADGHAAIFDHACJCFH
DFJHDJCEAGGEAICHEHGDKFCIGKDKEFCFFFIJKHGGECGIKKKA
AJACFFJJFCDGDIGAGCHJGKAHGEFIECACKIBGAAIBHDHIEGAF
GJIBDFIBHAGAFIDGKFGHCJAJICDJIBJEAJDAEBKGCFIJDEGC
HBABAJCHGFKGIBGBIEJDEHJAKFFHFBBHHHAIJIEDGKKBDKIJ
KIKJDKBIGCJAAIJHDAGFBEEKEFKGDHJDABCIGGBKDEFJDCKB
FDFJADGHAFFEFKIDKJBGIEHDCDGKJBICHCIAKDGBAKGCFKDI
IHJHAKDHGEJHHGGFEIHIFBEGHAJKGAEJDCAKHJKABJKGBBKB
GHBAHCIFFJGFBCACABIDFKGHHDJDKGEEIAIKKEGFHGFEBBCD
HIBADGAAHJAIAGGKCDHIBAAEKKCAHGEDDKEHHFFJGIJAHICE
CFGCAIBCDDECDJHKCJGJDKCBHDAHIKDJKAJJDADCFEKJKAFA
GCDEFHEJAIAJDGFDDADJGEIAJCKBICDGABKEDAFFGBIIHDBE
AIFDACKBJHBEEGIJDFBCJEKFBHICGGBFJCHIBJCIJIJCHAEF
KBEFKDCIEEDEBKIIBGDHGJFHGKCHJGCJFIGIDACBIGCJHKEK
CIICHEEIBGHHFCICKGECHJFBFKHBAKEGBCKCHKHKCKFDHJDG
GIKABBAIKJJDEECKBEECEHBHECDEGDICCGAGCCHCJAADBIFK
答案:
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(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- 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种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- select下拉框如何与接口配合动态生成option选项
前几天在做任务时考虑到这个问题,具体任务用例如下: HTML: JS:
- Tomcat&Servlet
Tomcat&Servlet 一.web开发相关的概念 1. 软件架构 1.1 C/S架构 C:Client客户端, S:Server服务器 比如:QQ.微信.大型网游 优点: 显示效果炫 安 ...
- unique key 唯一约束
#添加唯一约束mysql> alter table tb2 -> add unique key(name) ->;#删除唯一约束mysql> alter table ...
- 查询rabbitmq
package com.yunda.app.service; import java.io.InputStream; import java.net.HttpURLConnection; import ...
- 09-----JS事件流的概念(重点)
在学习jQuery的事件之前,大家必须要对JS的事件有所了解.看下文 时间的概念 HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以 ...
- Jenkins之自动触发部署之插件Generic Webhook Trigger Plugin
一.安装好插件 二.构建触发器会出现设置trigger的入口 三.设置的两个部分 第一: Jenkins的这个触发器,这里主要是接受post数据.其中Post content parameters是用 ...
- Java文件与io——字节数组流数据流字符串流
字节数组流 ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪read方法要提供的下一个字节.关闭ByteArrayInputStream无效. ...
- Java文件与io——字符流
Writer写入字符流的抽象类.对文件的操作使用:FileWriter类完成 Reader读取字符的抽象类. public class CharDemo { /** * @param args */ ...
- Murano Weekly Meeting 2015.07.21
会议时间: 2015.07.21 主持人: Kirill Zaitsev, core from Mirantis 会议摘要: 1.murano client和murano dashboard升级到y ...
- (转)Web服务器磁盘满故障深入解析
Web服务器磁盘满故障深入解析 原文:http://blog.51cto.com/oldboy/612351 ############################################# ...