题目:Farm Irrigation

题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494

题目思路:并查集

 #include<stdio.h>
//并查集重点就是实现:查询某点在哪个集合、将两个集合合并
//查找点的祖先
int a[];
int fun(int x)
{
int p=x;
// 初始化数组 a[i]=i;
// 也就是说当 a[i]==i 时,这是一个祖先,或者他是别人的子树
while(a[p]!=p)
{
p=a[p];
}
// p就是祖先,下面是路径压缩
int q=x;
while(a[q]!=p)
{
x=a[q];
a[q]=p;
q=x;
}
return p;
}
//合并两个集合
void join(int x,int y)
{
int xt=fun(x);
int yt=fun(y);
if(xt!=yt)
{
a[xt]=yt;
}
}
int n,m;
int bian(int i,int j)
{
return i*m+j;
}
int islian_heng(char c1,char c2)
{
if(c1=='B')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='D')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='F')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='G')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='I')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='J')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else if(c1=='K')
{
if(c2=='A'||c2=='C'||c2=='F'||c2=='G'||c2=='H'||c2=='I'||c2=='K') return ;
else return ;
}
else return ;
}
int islian_shu(char c1,char c2)
{
if(c1=='C')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='D')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='E')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='H')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='I')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='J')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else if(c1=='K')
{
if(c2=='A'||c2=='B'||c2=='E'||c2=='G'||c2=='H'||c2=='J'||c2=='K') return ;
else return ;
}
else return ;
}
int main()
{
char s[][];
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==-&&m==-) break;
for(int i=;i<n;i++)
scanf("%s",s[i]);
for(int i=;i<n*m;i++)
a[i]=i;
for(int i=;i<n;i++)
{
for(int j=;j<m-;j++)
{
if(islian_heng(s[i][j],s[i][j+])==)
{
join(bian(i,j),bian(i,j+));
}
}
}
for(int i=;i<m;i++)
for(int j=;j<n-;j++)
if(islian_shu(s[j][i],s[j+][i])==)
{
join(bian(j,i),bian(j+,i));
}
int co=;
for(int i=;i<n*m;i++)
{
if(a[i]==i) co++;
}
printf("%d\n",co);
}
return ;
}

AC代码

Farm Irrigation的更多相关文章

  1. ZOJ 2412 Farm Irrigation

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

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

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

  3. HDU1198水管并查集Farm Irrigation

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

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

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

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

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

  6. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  7. Farm Irrigation(非常有意思的并查集)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  8. ZOJ2412 Farm Irrigation(农田灌溉) 搜索

    Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB Benny has a spacious farm land to ...

  9. HDUOJ--------(1198)Farm Irrigation

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

随机推荐

  1. E - Intervals 贪心

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  2. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  3. Python之Requests库的7个主要方法

    方法 说明 requests.request() 构造一个请求,支撑一下各方法的基础方法 requests.get() 获取HTML网页的主要方法 requests.head() 获取网页头信息的方法 ...

  4. NodeJS的优缺点

    我们知道NodeJS是2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.Node.js对一些特殊用例进行优化,提供替代的API,使得V8在非浏览器环境下运行得更好,解 ...

  5. 联想Y700安装显卡驱动和CUDA8.0

    一显卡驱动安装 1 安装驱动安装软件 2 自动检测下载对应版本驱动 3下载完成后,点击开始,解压出来临时安装文件 由于是临时安装文件,退出就会关闭 4所以在没有关闭情况下,打开重新打开一次setup, ...

  6. Java中Enumeration接口的用法

    Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法,与Iterator差不多,用来遍历集合中的元素  但是枚举Enumeration只提供 ...

  7. i春秋-百度杯十月场-fuzzing

    1. 打开链接,提示 show me key,抓包,传值key=1,GET请求没有用,而POST请求有返回. 2.将md5值直接拿去解密,得到key=ichunqiu105    OK,进入下一步. ...

  8. 视觉机器学习------KNN学习

    KNN(K-Nearest Neighbor algorithm, K最近邻方法)是一种统计分类器,属于惰性学习. 基本思想:输入没有标签即未经分类的新数据,首先提取新数据的特征并与测试集中的每一个数 ...

  9. 20175310 《Java程序设计》第8周学习总结

    20175310 <Java程序设计>第8周学习总结 本周博客: https://www.cnblogs.com/xicyannn/p/10722004.html 教材学习内容总结 这周学 ...

  10. WPF Binding学习(四) 绑定各种数据源

    转自:http://blog.csdn.net/lisenyang/article/details/18312199 1.集合作为数据源 首先我们先创建一个模型类 public class Stude ...