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 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
随机推荐
- spring boot 开发非web应用程序
- cakePHP 分页栏
<div class="page"><?php if ($total > 1) echo $this->element('page_list', ar ...
- PHPActiveRecord 学习二
ORM关联查询 a.一对多 针对外键来说 谁属于谁 谁有谁 user表 CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `na ...
- ss源码学习--工作流程
ss的local端和server端的工作流程相似,因此复用了TCPRelay类和TCPRelayHandler类. 两端均是使用TCPRelay类监听连接,并使用TCPRelayHandler类处理请 ...
- sqlserver自增主键
参考 https://www.cnblogs.com/michellexiaoqi/p/8031294.html 1.选中表: 2.右击鼠标,设计: 3.选中列(整数类 ...
- Ubuntu 16.04下添加新用户
某些情况下,Ubuntu 使用useradd 新用户名,在home 文件夹下面看不到新创建的用户文件夹,例如:root@worker:/home/kuku# useradd spark root@ ...
- 准备面试-DFT
问题:面试DFT岗位的准备工作 1.在EETOP上搜索DFT看到的一些要求 1.要弄明白DCSCAN.ACSCAN.MBIST.边扫等原理, 2.要会利用相应的Synopsys或Mentor公司工具! ...
- leecode 937 Reorder Log Files (模拟)
传送门:点我 You have an array of logs. Each log is a space delimited string of words. For each log, the ...
- Adapter类 调用Activity中的函数
在Adapter类中可以定义一个MainActivity变量,在初始化时,对其赋值,例如fragment的适配器中: private MainActivity context; private Lis ...
- cipher的各个模式
block cipher 工作模式(引自百度)Electronic Codebook Mode 最经典的模式,把明文按64比特为单位分为block, 对所有block使用同样的密钥来加密,最后把输出的 ...