令人蛋疼的并查集……

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

这个题有些像一个叫做“水管工”的游戏。给你一个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. POJ 1724 Roads

    题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程         注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijks ...

  2. Hibernate3中将指定的HQL语句转换成SQL语句

    import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.hql.ast.QueryTranslatorI ...

  3. [2-sat]HDOJ1824 Let's go home

    中问题 题意略 和HDOJ 3062一样 这里 每个队员都有 选 和 不选 两种, 即 上篇所说的$x$和$x’$ 建图:队长(a)留下或者其余两名队员(b.c)同时留下 那么就是$a' \Right ...

  4. Linux磁盘管理命令

    1.磁盘分割: fdisk [root@linux ~]# fdisk [-l] 装置名称 参数: -l :输出后面接的装置所有的partition内容.若仅有fdisk -l时, 则系统将会把整个系 ...

  5. Visual StudioTools for Unity 使用技巧2

    在之前的博客介绍了 Visual Studio Tools for Unity的安装和使用. http://www.cnblogs.com/petto/p/3886811.html 其实这个工具还提供 ...

  6. ReadDirectoryChangesW 监控文件夹 (一个简单的监控示例程序)(文件被修改了,也可以探测到)

    // .h文件 #pragma once typedef void (*PFN_NotifyAction)(DWORD dwAction, LPWSTR szFile, DWORD dwLength) ...

  7. RESTful WebService入门

    RESTful WebService入门   RESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状 ...

  8. Vim的可视模式

    可视模式可以看到选中的字符串, 并对其进行操作 v:进入字符选择模式 V:进入行选择模式 ctrl-v(Window是ctrl-q):进入block选择模式 o:移动光标到选择的另一端 O:移动光标到 ...

  9. RegexOne

    http://regexone.com/ http://regexone.com/lesson/optional_characters? http://regexone.com/lesson/capt ...

  10. NuGet在2015中的使用

    NuGet Package Restore  https://docs.nuget.org/Consume/Package-Restore 以https://github.com/andburn/hd ...