hdu 1198 Farm Irrigation
令人蛋疼的并查集……
我居然做了大量的枚举,居然过了,我越来越佩服自己了
这个题有些像一个叫做“水管工”的游戏。给你一个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的更多相关文章
- 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) ...
- 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(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation (并查集优化,构图)
本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- HDU 2412 Farm Irrigation
题目: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
随机推荐
- Linux性能监控分析命令
vmstat sar iostat top free uptime netstat ps strace lsof
- 学习笔记--Git安装 创建版本库 图文详解
一.Git下载 在Windows上安装git,一般为msysgit,官网地址:http://git-scm.com/ 我下载的是Git-1.9.2-preview20140411.exe 二.Git安 ...
- Git stash方法(转)
命令:git stash1.使用git stash保存当前的工作现场,那么就可以切换到其他分支进行工作,或者在当前分支上完成其他紧急的工作,比如修订一个bug测试提交. 2.如果一个使用了一个git ...
- 深入浅出Java并发包—锁机制(一)
前面我们看到了Lock和synchronized都能正常的保证数据的一致性(上文例子中执行的结果都是20000000),也看到了Lock的优势,那究竟他们是什么原理来保障的呢?今天我们就来探讨下Jav ...
- eclipse的设置和优化
转载:http://my.oschina.net/zhaoqian/blog/66545 1.eclipse下的编码设置: eclipse 中使用模板新建 JSP,xhtml等 文件时,默认的编码为: ...
- TCL语言笔记:TCL中的String命令
一.介绍 字符串是 Tcl 中的基本数据类型,所以有大量的字符串操作命令.一个比较重要的问题就是模式匹配,通过模式匹配将字符串与指定的模式(格式)相匹配来进行字符串的比较.搜索等操作. 二.strin ...
- Java文件解压
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- !!流行的php面试题及答案
分类: 1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量(1)中:而链接到当前页面的URL记录在预定义变量(2)中. 答:echo $_SERVER['PHP_SELF']; ...
- UVa 10075 - Airlines
航线算球面距离,需要经纬度转空间坐标. 任意两点间距离用Floyd求出来,查询时直接查表. #include <cstdio> #include <map> #include ...
- AsciiDoc Markup Syntax Summary
AsciiDoc Markup Syntax Summary ============================== A summary of the most commonly used ma ...