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 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
随机推荐
- C# 无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误解决办法
“/CRM”应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法识别的属性“targetF ...
- tomcat生成调试日志配置
创建文件logging.properties 文件存放于应用WEB-INF/classes下 文件内容如下: org.apache.juli.FileHandler.prefix = error-d ...
- selenium自动化测试安装,浏览器驱动版本对应
- 关于git经常忘记的:远程仓库关联。
我们有时习惯建立好工程后再传到git上,这是时候就忘记咋弄啦, 其实,只要配置远程仓库就行: git remote add +url...具体看网上哦,这里提醒下 Git clone远程分支 Git ...
- Illegal access: this web application instance has been stopped already. could not load **
启动tomcat的时候会报这样的错误: Illegal access: this web application instance has been stopped already. could n ...
- Controlled Components
[Controlled Components] In HTML, form elements such as <input>, <textarea>, and <sele ...
- 外购半成品报SHORT问题(非验货客户)
外购半成品报SHORT问题(验货客户)https://www.cnblogs.com/Snowfun/p/8660646.html 下面看非验货客户: 1.检查采购类型是否为F(SAP_MARC),为 ...
- mysql 定时备份任务
备份方案: 本地备份并同步至远程服务器,保留30天数据 1. 本地数据库备份,备份数据库gold_ecooy,naiang#!/bin/bash#xliang#Created Time: 2018-1 ...
- python全栈开发 随笔 'is' 和 == 的比较知识与区别 编码和解码的内容及转换
python 一. is 和 == 的区别; == 比较的是两边的值. a = 'alex' b = 'alex' print(a = b) #True a = 10 b = 10 print(a = ...
- mongodb-参考其他
MongoDB教程 http://www.runoob.com/mongodb/mongodb-window-install.html