hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198
简单并查集
分别合并竖直方向和水平方向即可
代码:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#define maxn 110
using namespace std;
int parent[maxn*maxn];
char map[maxn][maxn];
int find(int x)
{
int s;
for(s=x;parent[s]>=;s=parent[s]);
while(s!=x)
{
int tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void Union(int r1,int r2)
{
int R1=find(r1);
int R2=find(r2);
int tmp=parent[R1]+parent[R2];
if(parent[R1]>parent[R2])
{
parent[R1]=R2;
parent[R2]=tmp;
}
else
{
parent[R2]=R1;
parent[R1]=tmp;
}
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m< || n<) break;
for(int i=;i<m;i++)
scanf("%s",map[i]);
for(int i=;i<m*n;i++)
parent[i]=-;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(i> && (map[i][j]=='A' ||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K'))
if(map[i-][j]=='C'||map[i-][j]=='D'||map[i-][j]=='E'||map[i-][j]=='H'||map[i-][j]=='I'||map[i-][j]=='J'||map[i-][j]=='K')
Union(i*n+j,(i-)*n+j);
if(j>&&(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K'))
if(map[i][j-]=='B'||map[i][j-]=='D'||map[i][j-]=='F'||map[i][j-]=='G'||map[i][j-]=='I'||map[i][j-]=='J'||map[i][j-]=='K')
Union(i*n+j,i*n+j-); }
int ans=;
for(int i=;i<m*n;i++)
if(parent[i]<) ans++;
cout<<ans<<endl;
}
return ;
}
hdu1198 Farm Irrigation 并查集的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- Farm Irrigation(并查集)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- hdu1198 Farm Irrigation —— dfs or 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- Farm Irrigation(非常有意思的并查集)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
随机推荐
- strtol函数 将字符串转换为相应进制的整数
转自http://hi.baidu.com/qwpsmile/blog/item/9bc44efa4f41018a9f514637.html +----------------+| strt ...
- js,jq获取元素位置属性及兼容性写法
网页被卷起来的高度/宽度 document.documentElement.scrolltop //火狐 和 其他浏览器 document.body.scrolltop //ie,谷歌浏览器和没有 ...
- 【2017-03-30】JS-document对象
一.获取标记对象 1.document.getElementById("id"); 根据id找,最多找到一个. 2.document.getElementsByNa ...
- git工具使用方法及常用命令
git下载地址:https://git-for-windows.github.io/git环境变量配置:增加:GIT_HOME = C:\Git (文件夹为git软件的安装文件夹)Path添加: %G ...
- jQuery ajax 与服务器交互方法
1.HTML <table> <tr> <td>用户名:</td> <td><input type="text" ...
- 【Electron】Electron开发入门(四):操作PC端文件系统
一.调用PC端默认方式打开本地文件 在main.js里 // 打开系统本地文件或者网页链接 const {shell} = require('electron'); // Open a local f ...
- 老李推荐:第4章1节《MonkeyRunner源码剖析》ADB协议及服务: ADB协议概览 1
老李推荐:第4章1节<MonkeyRunner源码剖析>ADB协议及服务: ADB协议概览 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试, ...
- 手机自动化测试:appium问题解决
手机自动化测试:appium问题解决 Appium遇到问题: 问题一:问题org.openqa.selenium.remote.UnreachableBrowserException: Could ...
- 性能测试分享:MYSQL死锁
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
- html中的Flash对象
开源Flash播放器 http://www.open-open.com/ajax/Video.htm