题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198

dfs:

 #include<cstdio>//hdu1198 dfs
#include<cstring> int well[][] = { {,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,},{,,,,}}; const int ca[][] = {,,,,,,,,,}, mov[][] = {,,-,,,,,-};
int m,n,sum, map[][],id[][]; void build(int set, int x, int y)
{
for(int k = ; k<; k++)
{
if(well[set][k])
{
map[*x+ca[k][]][*y+ca[k][]] = ;
}
}
} void dfs(int x, int y, int iden)
{
id[x][y] = iden;
for(int i = ; i<; i++)
{
int xx = x + mov[i][], yy = y + mov[i][];
if(xx< || xx>*m- || yy< || yy>*n- ) continue;
if(map[xx][yy] && !id[xx][yy])
dfs(xx,yy,iden);
}
} int main()
{
char init[];
while(scanf("%d%d",&m,&n) && (m!=- || n!=- ))
{
memset(map,,sizeof(map));
memset(id,,sizeof(id));
for(int i = ; i<m; i++)
{
scanf("%s",init);
for(int j = ; j<n; j++)
build(init[j]-,i,j);
}
sum = ;
for(int i = ; i<=*m-; i++)
{ for(int j = ; j<=*n-; j++)
{
//printf("%d ",map[i][j]);
if(map[i][j] && !id[i][j])
{
dfs(i,j,++sum);
}
}
//putchar('\n');
} printf("%d\n",sum); }
return ;
}

并查集:

 #include<cstdio>//hdu1198 并查集 每个格子的标号为i*n+j,初始father也为i*n+j,以次来作为合并集合的依据
#include<cstring> int n,m,father[][];
char map[][];
char type[][]={"","","","","","",
"","","","",""}; int find(int x)
{
return (x==father[x/n][x%n])?x:find(father[x/n][x%n]);
} int unit(int a, int b)
{
a = find(a);
b = find(b);
if(a!=b)//合并集合
father[a/n][a%n] = b;
} int judge(int i, int j)
{ //判断是否与左边相连
if(j> && type[map[i][j]-'A'][]=='' && type[map[i][j-]-'A'][]=='')
unit(i*n+j,i*n+j-);
//判断是否与上边相连
if(i> && type[map[i][j]-'A'][]=='' && type[map[i-][j]-'A'][]=='')
unit(i*n+j,(i-)*n+j);
} int main()
{
while(scanf("%d%d",&m,&n) && (m!=- || n!=-))
{
for(int i = ; i<m; i++)
{
scanf("%s",&map[i]);
for(int j = ; j<n; j++)
{ //初始化为自己
father[i][j] = i*n+j;
}
} //并查集过程
for(int i = ; i<m; i++)
for(int j = ; j<n; j++)
judge(i,j); int sum = ;
for(int i = ; i<m; i++)
for(int j = ; j<n; j++)//看看有多少个“根节点”
if(father[i][j]==i*n+j) sum++; printf("%d\n",sum);
} }

hdu1198 Farm Irrigation —— dfs or 并查集的更多相关文章

  1. hdu1198 Farm Irrigation 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...

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

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

  3. acdream 1685 多民族王国(DFS,并查集)

    Problem Description 娜娜好不容易才回忆起自己是娜娜而不是什么Alice,也回忆起了自己要继续探索这个世界的目标,便偷偷溜出皇宫.娜娜发现这个王国有很多个民族组成,每个民族都有自己的 ...

  4. HDU 5438 Ponds (DFS,并查集)

    题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和. 析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然 ...

  5. 判图的连通性(dfs,并查集)

    一.无向图 欧拉回路:每个顶点度数都是偶数 欧拉路:所有点度数为偶数,或者只有2个点度数为奇数 当然判连通性 hdu 1878 欧拉回路 两种判连通的方法 dfs #include <iostr ...

  6. ZOJ 2412 Farm Irrigation(DFS 条件通讯块)

    意甲冠军  两个农田管内可直接连接到壳体  他们将能够共享一个水源   有11种农田  管道的位置高于一定  一个农田矩阵  问至少须要多少水源 DFS的连通块问题  两个相邻农田的管道能够直接连接的 ...

  7. 蓝桥杯历届试题 危险系数(dfs或者并查集求无向图关于两点的割点个数)

    Description 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个 ...

  8. Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

随机推荐

  1. 网络数据嗅探工具HexInject

    网络数据嗅探工具HexInject   网络数据嗅探是渗透测试工作的重要组成部分.通过嗅探,渗透人员可以了解足够多的内容.极端情况下,只要通过嗅探,就可以完成整个任务,如嗅探到支持网络登录的管理员帐号 ...

  2. 想用idea的update classes and resources找不到了,热部署无法使用

    发现为了方便调试页面,想用idea的update classes and resources找不到了,发现需要把deployment选择exploded 的那个war包,才能在service--> ...

  3. Android CrashHandler

    package jason.android.utils; import android.content.Context; import android.content.pm.PackageInfo; ...

  4. 讯飞语音识别Android-Demo

    import java.io.UnsupportedEncodingException; import android.app.Activity; import android.os.Bundle; ...

  5. lstm公式推导

    http://blog.csdn.net/u010754290/article/details/47167979 导言 在Alex Graves的这篇论文<Supervised Sequence ...

  6. 写一个dup2功能同样的函数,不能调用 fcntl 函数,而且要有出错处理

    实现的时候用到系统原来的dup函数 // mydup2.c // 2015/08/17 Lucifer Zhang version1.0 // write my own dup2 function / ...

  7. Naive Bayesian文本分类器

    贝叶斯学习方法中有用性非常高的一种为朴素贝叶斯学习期,常被称为朴素贝叶斯分类器. 在某些领域中与神经网络和决策树学习相当.尽管朴素贝叶斯分类器忽略单词间的依赖关系.即如果全部单词是条件独立的,但朴素贝 ...

  8. C# 将long类型写入二进制文件用bw.Write(num);将其读出用long num= br.ReadInt64();

    理由: 因为long类型是 System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方) 而long BinaryReader ...

  9. Content Provider 详解

    几个概念:Cursor. Content provider . Uri  .contentresolver 1. Cursor : 个人理解为数据库中的一行数据,它是每行数据的集合.它是一个类.通过它 ...

  10. python(10)- 字符编码

    一 什么是编码? 基本概念很简单.首先,我们从一段信息即消息说起,消息以人类可以理解.易懂的表示存在.我打算将这种表示称为“明文”(plain text).对于说英语的人,纸张上打印的或屏幕上显示的英 ...