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种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
随机推荐
- 关于SqlDataReader使用的一点疑惑
C#中的SqlDataReader类(System.Data.SqlClient)是用来在保持打开数据库连接的状态下取数据用的 用法如下图: “保持与数据库的连接”这个特性也是SqlDataReade ...
- 互不侵犯king (状压dp)
互不侵犯king (状压dp) 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.\(1\le n\ ...
- xml 的使用和解析 及解析工具
xml 一.xml简介 1. 什么是xml XML:Extensiable Markup Language,可扩展标记语言.和HTML有语法相似之处,也有作用上的不同: 和html相似: 都是由一堆标 ...
- boost asio one client one thread
总结了一个简单的boost asio的tcp服务器端与客户端通信流程.模型是一个client对应一个线程.先做一个记录,后续再对此进行优化. 环境:VS2017 + Boost 1.67 serve ...
- Start and Stop Bitbucket Server
Starting and stopping Bitbucket Server This page describes the various ways you can start or stop Bi ...
- PHPExcel探索之旅---阶段三 绘制图表
利用PHPExcel插件进行绘制图表 <?php /** * 使用PHPExcel插件在excel文件中利用其中的数据建立折线图 * */ $dir = dirname(__FILE__); r ...
- CF213E Two Permutations 线段树维护哈希值
当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$ 由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$; 如何判断呢?可以用哈希$qwq$: 我们用线段树维护哈 ...
- spring boot之 Bean的初始化和销毁(4)
原文:https://blog.csdn.net/z3133464733/article/details/79189699 -------------------------------------- ...
- android 缓存路径
用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还保 ...
- ASP.NET相关
1.委托:把一个方法当作参数传到另一个方法中 扩展方法:1.静态类 2.静态方法 3.this关键字 using System; using System.Collections.Generic; u ...