题目: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. MySQL高级知识(四)——Explain

    前言:explain(执行计划),使用explain关键字可以模拟优化器执行sql查询语句,从而知道MySQL是如何处理sql语句.explain主要用于分析查询语句或表结构的性能瓶颈. 注:本系列随 ...

  2. 因PHP漏洞,超过4.5万个中国网站被攻击

    因PHP漏洞,超过4.5万个中国网站被攻击 日前据外媒ZDNet了解,超过4.5万个中国网站受到了试图进入网络服务器的不法分子攻击. 此次黑客利用中国网络安全公司VulnSpy在ExploitDB上发 ...

  3. Python:Day40 html

    URL包括三个部分:协议.域名.路径 htyper text markup language (html)  即超文本标记语言 前端一共包括三个内容:html.css.js html做为基础,让CSS ...

  4. (1)ESP8266微信门铃

    http://rayuu.com/2017/11/13/esp8266-wechat-doorbell/(留做参考) 就是当门铃按键按下,微信会收到消息提醒. 若在家就算了,没在家会受到远程提示. 自 ...

  5. Topshelf:一款非常好用的 Windows 服务开发框架 转发https://www.cnblogs.com/happyframework/p/3601995.html

    背景 多数系统都会涉及到“后台服务”的开发,一般是为了调度一些自动执行的任务或从队列中消费一些消息,开发 windows service 有一点不爽的是:调试麻烦,当然你还需要知道 windows s ...

  6. 重建UNDO表空间遭遇ORA-01548

    今天开发那边的一套数据库的undo表空间不知道被谁设置成了自动扩展,然后所谓的屋漏偏逢连夜雨的是, 开发人员今天跑了一个很大的事物,然后直接后果就是undo表空间不断被扩展,直到把文件系统写爆了.没办 ...

  7. python3 内置函数

    '''iter()和next()'''# lst = [1, 2, 3]# it = iter(lst)# print(it.__next__())# print(it.__next__())# pr ...

  8. Linux并发与同步专题 (3) 信号量

    关键词:Semaphore.down()/up(). <Linux并发与同步专题 (1)原子操作和内存屏障> <Linux并发与同步专题 (2)spinlock> <Li ...

  9. python调用数据返回字典dict数据的现象2

    python调用数据返回字典dict数据的现象2 思考: 话题1连接:https://www.cnblogs.com/zwgbk/p/10248479.html在打印和添加时候加上内存地址id(),可 ...

  10. Vue-校验props传来的值

    对父组件传来的值进行校验. Vue.component('child',{ props:{ content:{ type:String, required:false, default:'li zha ...