题目链接: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. ML | spectral clustering

    What's xxx In multivariate statistics and the clustering of data, spectral clustering techniques mak ...

  2. 0.从零开始搭建spring mvc + mybatis + memcached+ dubbo\zookper的maven项目

    1.首先创建maven 项目,配置相关pom信息 2.配置spring mvc 4, 测试,提交代码 3.引入配置mybatis3,测试,提交代码 4.配置事务,测试,提交代码 5.配置memcach ...

  3. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

  4. 前端必备性能知识 - http2.0

    前端开发中,性能是一定绕不开的,今天就来说一下前后台通信中最重要的一个通道--HTTP2.0 最开始的通讯协议叫http1.0,作为始祖级的它,定义了最基本的数据结构,请求头和请求体,以及每一个字段的 ...

  5. margin: 0 auto; 元素水平居中布局无效

    失效原因: 要给居中的元素一个宽度,否则无效. 该元素一定不能浮动或绝对定位,否则无效. 在HTML中使用<center></center>标签,需考虑好整体构架,否者全部元素 ...

  6. 【Nginx】事件驱动框架和异步处理

    Nginx对请求的处理是通过事件触发的,模块作为事件消费者,仅仅能被事件收集.分发器调用.这与传统的Webserver是不同的. 传统的Webserver下,一个请求由一个进程消费.请求在建立连接后将 ...

  7. PS常用平面设计制作尺寸

      PHOTPSHOP照片处理    数码照片尺寸 平面设计常用制作尺寸 名片 横版:90*55mm<方角> 85*54mm<圆角> 竖版:50*90mm<方角> ...

  8. mysql 授权新的root用户

    grant all privileges to *.* on system@'localhost' identified by 'woshishui' with grant option;

  9. angular 资源路径问题

    1.templateUrl .component("noData",{ templateUrl:"components/noData.html" // 注意相对 ...

  10. DataGridView.DataSource= list(Of T)

    注:本文样例的代码承接上篇文章:DataTable填充实体类返回泛型集合. 在D层查询完毕之后.我们将DataTable转化为泛型集合.然后经过中间各层,返回U层.到了这里,问题来了.我们这时候要将这 ...