题目: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. 如何用Ant Design Pro框架做项目省力

    1.熟悉React所有语法,以及redux.redux-saga.dva.一类的库的能力 2.灵活运用该框架提供的基础UI组件,想方设法利用现有的UI组件进行组合,尽可能减少工作量

  2. HashMap源码调试——认识"put"操作

    前言:通常大家都知道HashMap的底层数据结构为数组加链表的形式,但其put操作具体是怎样执行的呢,本文通过调试HashMap的源码来阐述这一问题. 注:jdk版本:jdk1.7.0_51 1.pu ...

  3. LDAP概念和原理

    LDAP概念和原理介绍 相信对于许多的朋友来说,可能听说过LDAP,但是实际中对LDAP的了解和具体的原理可能还比较模糊,今天就从“什么是LDAP”.“LDAP的主要产品”.“LDAP的基本模型”.“ ...

  4. CCF-201803-3-URL映射(模拟)

    Problem CCF-201803-3-URL映射 Time Limit: 1000 mSec Problem Description URL 映射是诸如 Django.Ruby on Rails ...

  5. maven install 错误

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-c ...

  6. Spring Security(二十二):6.4 Method Security

    From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...

  7. 移走mysql data目录,及常见mysql启动问题

    一般mysql安装在/usr/local/下,现以将/usr/local/mysql/data目录移动到/home/mysql下为例 首先保证/home/mysql目录是存在的,本例中使用了mysql ...

  8. P1654 OSU!-洛谷luogu

    传送门 题目背景 原 <产品排序> 参见P2577 题目描述 osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败 ...

  9. 一道很有意思的java线程题

    这几天看结城浩的<java多线程设计模式>,跟着做一些习题,有几道题目很有意思,记录下自己的体会. 首先是题目(在原书212页,书尾有解答): public class Main { pu ...

  10. Linux kprobe调试技术使用

    kprobe调试技术是为了便于跟踪内核函数执行状态所设计的一种轻量级内核调试技术. 利用kprobe技术,可以在内核绝大多数函数中动态插入探测点,收集调试状态所需信息而基本不影响原有执行流程. kpr ...