令人蛋疼的并查集……

我居然做了大量的枚举,居然过了,我越来越佩服自己了

这个题有些像一个叫做“水管工”的游戏。给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们连接。然后找这里面有多少个连通图。

给大家一个一点也不高大上,但是一眼就能看懂的代码吧……

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int N = ; char mpc[N][N]; //第一次输入
bool mp[N*N][N*N]; //整理单向连通
bool mps[N*N][N*N]; //记录双向连通
int fm[N*N]; //并查集父节点,不用多说了吧……
int m, n;
int sum; void change2(int k, int x, int y) //四个方向连通
{
switch(k)
{
case :
if(x- >= ) mp[x*m+y][(x-)*m+y] = ; break;
case :
if(y- >= ) mp[x*m+y][x*m+y-] = ; break;
case :
if(x+ < n)mp[x*m+y][(x+)*m+y] = ; break;
case :
if(y+ < m) mp[x*m+y][x*m+y+] = ; break;
}
} void change(int x, int y) //11个图的连接方式
{
switch(mpc[x][y]-'A')
{
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
change2(, x, y);
break;
case :
change2(, x, y);
change2(, x, y);
change2(, x, y);
change2(, x, y);
break;
}
} int fd(int x) //寻找父节点
{
while(x != fm[x])
{
x = fm[x];
}
return x;
} void link(int x, int fx) //合并+压缩
{
int mx;
while(x != fm[x])
{
mx = x;
x = fm[x];
fm[mx] = fx;
}
fm[x] = fx;
} void jp(int x, int y)
{
int fx = fd(x);
int fy = fd(y);
if(fx != fy)
{
link(x, fx);
link(y, fx);
sum++; //统计连接的点的个数
} } int main()
{
//freopen("test.txt", "r", stdin);
while(~scanf("%d%d", &n, &m) && n != - && m != -)
{
memset(mp, , sizeof(mp));
memset(mps, , sizeof(mps));
memset(mpc, , sizeof(mpc));
for(int i = ; i < m*n; i++) fm[i] = i; for(int i = ; i < n; i++)
{
scanf("%s", mpc[i]);
for(int j = ; j < m; j++)
{
change(i, j);
}
}
for(int i = ; i < n*m; i++)
{
for(int j = ; j < i ; j++)
{
if(mp[i][j] && mp[j][i]) mps[i][j] = ;
}
} sum = ;
for(int i = ; i < n*m; i++)
{
for(int j = ; j < i; j++)
{
if(mps[i][j]) jp(i, j);
}
}
//for(int i = 0; i < m*n; i++) if(fm[i] == i) sum++; printf("%d\n", n*m-sum);
}
return ;
}

对了,这个题dfs也能做,只是我不想再写了……

hdu 1198 Farm Irrigation的更多相关文章

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

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

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

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

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

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

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

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

  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 || 并查集)

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

  7. HDU 1198 Farm Irrigation (并查集优化,构图)

    本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...

  8. hdu 1198 Farm Irrigation(并查集)

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

  9. HDU 2412 Farm Irrigation

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

随机推荐

  1. 【C++基础】指针好难啊,一点点啃——基本概念

    指针保存的是另一个对象的地址(概念真的很重要!!) ; int *ptr = &a;//*定义一个指向int类型的指针ptr, &a取变量a的地址 引用是对象的别名,多用于函数形参,引 ...

  2. U盘文件夹被病毒隐藏,且不能取消解决办法

    在cmd下进入到U盘,运行attrib -r -a -s -h *.* /s /d

  3. 李洪强漫谈iOS开发[C语言-007]-语言标准简介

    C语言是介于低级语言和高级语言之间的 一个应用程序 C语言在嵌入式上使用,的确是具有低级语言的特征 直接操作硬件,扫描内存 访问到的都是虚拟内存,一个应用程序占多大内存? 表示最多 可以放多少条指令 ...

  4. QEvent大全,有中文解释

    简述 QEvent 类是所有事件类的基类,事件对象包含事件参数. Qt 的主事件循环(QCoreApplication::exec())从事件队列中获取本地窗口系统事件,将它们转化为 QEvents, ...

  5. Java API —— Pattern类

    正则表达式     写一个功能实现QQ号码的校验. import java.util.Scanner; public class RegexDemo01 { public static void ma ...

  6. !!无须定义配置文件中的每个变量的读写操作,以下代码遍历界面中各个c#控件,自动记录其文本,作为配置文件保存

    namespace PluginLib{    /// <summary>    /// 遍历控件所有子控件并初始化或保存其值    /// </summary>    pub ...

  7. $.post()

    定义和用法 post() 方法通过 HTTP POST 请求从服务器载入数据. jQuery.post(url,data,success(data, textStatus, jqXHR),dataTy ...

  8. oracle .bash_profile

    [oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...

  9. Android Studio:libpng warning: iCCP: Not recognizing known sRGB profile that has been edited解决办法

    把以前的eclipse的项目导入Android Studio中,Build项目的时候,出现了一堆错误. 如下: AAPT err(Facade for 1944774242): ERROR: 9-pa ...

  10. Mac下安装HBase及详解

    Mac下安装HBase及详解 1. 千篇一律的HBase简介 HBase是Hadoop的数据库, 而Hive数据库的管理工具, HBase具有分布式, 可扩展及面向列存储的特点(基于谷歌BigTabl ...