hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198
有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井
比如
ADC
FJK
IHE是下图这种情况

需要打三口井,其实想多了就是集合合并差不多,用并查集可以解决,将每个格子按顺序是从0到n*m-1,然后根据连接情况判断是否相连然后合并
第一次写成了暴力的,虽然一遍过了,但是觉得太长了,然后又改成了下面简洁并查集的,还有下下面的dfs的
并查集
#include<cstdio>
using namespace std;
char yj[][];
int father[*+];
int dir[][]={{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,}};
void give(int x)
{
for (int i=;i<x;i++)
father[i]=i;
}
int _find(int x)
{
if (x==father[x]) return father[x];
father[x]=_find(father[x]);
return father[x];
}
int merge(int x,int y)
{
int sx=_find(x);
int sy=_find(y);
if (sx!=sy)
father[sx]=sy;
}
int main()
{
int n,m,i,j;
while (~scanf("%d %d",&n,&m))
{
if (n==-&&m==-) break;
for (i=;i<n;i++)
scanf("%s",yj[i]);
give(n*m);
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
int x=yj[i][j]-'A';
int y=yj[i][j+]-'A';
if (dir[x][]==&&dir[y][]==&&j+<m)
merge(i*m+j,i*m+j+);
y=yj[i+][j]-'A';
if (dir[x][]==&&dir[y][]==&&i+<n)
merge(i*m+j,(i+)*m+j);
}
}
int sum=;
for (i=;i<n*m;i++)
if (father[i]==i)
sum++;
printf("%d\n",sum);
}
return ;
}
dfs
#include<cstdio>
#include<cstring>
using namespace std;
int dx[]={,-,,};//下上左右
int dy[]={,,-,};
char yj[][];
int ans,vis[][],n,m;
int dir[][]={{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},
{,,,},{,,,}};
int check(int x,int y,int di)
{
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
if (dir[x][]&&dir[y][]&&di==) return ;
return ;
}
void dfs(int x,int y)
{
int i;
if (vis[x][y]) return;
vis[x][y]=;
//printf("%d %d\n",x,y);
for (i=;i<;i++)
{
int sx=x+dx[i];
int sy=y+dy[i];
if (sx<||sx>=n||sy<||sy>=m) continue;
if (vis[sx][sy]) continue;
int q=yj[sx][sy]-'A';
int w=yj[x][y]-'A';
if (check(q,w,i))
dfs(sx,sy);
}
}
int main()
{
int i,j;
while (~scanf("%d %d",&n,&m))
{
if (n==-&&m==-) break;
for (i=;i<n;i++)
scanf("%s",yj[i]);
memset(vis,,sizeof(vis));
ans=;
for (i=;i<n;i++)
{
for (j=;j<m;j++)
{
if (vis[i][j]) continue;
dfs(i,j);
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
hdu 1198 (并查集 or dfs) Farm Irrigation的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- hdu-1272 小希的迷宫---并查集或者DFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 题目大意: Problem Description 上次Gardon的迷宫城堡小希玩了很久(见 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
随机推荐
- mysql 远程 ip访问
默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: 如下的方式确认: root#mysql -h localhost-u ...
- Swift字符串常用方法
1.0 比较两个字符串是否相等 判断字符串相等的方法是: ==. var str1 = "Hello world" var str2 = "Hello world&quo ...
- kubernetes ui 搭建
1.部署Kubernetes云计算平台,至少准备两台服务器,此处为3台 Kubernetes Master节点:192.168.0.111 Kubernetes Node1节点:192.168.0.1 ...
- vue.js入门操作
1.vue框架是经典的MVVM模式, .vue文件是模板文件模板文件又分为3个部分一 <template></template>(html部分)二 <script> ...
- jq控制select值为某个时选中
$("#selectche_type option").each(function(){//用id ,用class好像不得,不知道为何 alert(2) if($(this).va ...
- mongo的csv文件参考
https://blog.csdn.net/u012318074/article/details/77713228
- java课后作业总结
今天的课后作业是做一个查找一段文章中单词出现的概率,统计并 输出出现概率最高的几个单词.拿到题目,老师还是往常一样提醒着我们先分析题目,构建大概的编程思路.程序需要遇到文本文件的输入输出操作,这一直一 ...
- common mistake of closure in loops
[common mistake of closure in loops] 下例中item引用的始终是最后一个值. function showHelp(help) { document.getEleme ...
- EF CodeFirst学习笔记002--更新数据库表
BlogEntities这个类从DbContext继承,负责数据库的更新. Database.SetInitializer(new DropCreateDatabaseIfModelChanges&l ...
- extJS 动态引用加载(转)
ExtJs有庞大的类型库,很多类可能在当前的页面根本不会用到,我们可以引入动态加载的概念来即用即取.这些代码都要写在Ext.onReady外面. 1.动态引用外部Js //加载配置可用 Ext.Loa ...