hdu 5652(并查集)
题意:很久之前,在中国和印度之间有通路,通路可以简化为一个n*m的字符串,0表示能通过,1表示障碍,每过一年就有一个坐标变成1,问你什么时候,通路彻底无法通过;
解题思路:无向图的连通性,一般直接搜索或者并查集判定,这里用的是并查集,我们把相连的障碍放到一个集合内,然后如果第一列的某点和最后一列的某点也在这个集合内,那么说明,这个通路被障碍堵死了;
这里要处理下第一列和最后一列的问题,可以选择
#include<iostream>
#include<algorithm>
#define N 605
using namespace std;
int next1[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int dx[]={-1,-1,-1,0,0,1,1,1};
int dy[]={-1,0,1,-1,1,-1,0,1};
int f[N*N];
char a[N][N];
int findf(int x)
{
if(f[x]==x)
return x;
else findf(f[x]);
}
void join(int x,int y)
{
int t1=findf(x);
int t2=findf(y);
if(t1!=t2)
f[t2]=t1;
}
int main()
{
int tx,ty;
int n,m;
int t;
int tt;
cin>>t;
while(t--)
{
cin>>n>>m;
int s,e;
s=n*m;e=s+1;
for(int i=0;i<=e;i++)
f[i]=i;
for(int i=0;i<n;i++)
{
join(s,m*i);join(e,m*i+m-1);
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>a[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(a[i][j]=='0')
continue;
for(int k=0;k<8;k++)
{
tx=i+dx[k];ty=j+dy[k];
if(tx<0||ty<0||tx>=n||ty>=m||a[tx][ty]=='0')
continue;
join(i*m+j,tx*m+ty);
}
}
cin>>tt;
int x,y;
int flag=-1;
for(int i=1;i<=tt;i++)
{
cin>>x>>y;
if(flag!=-1)
continue; a[x][y]='1';
for(int k=0;k<8;k++)
{
tx=x+dx[k];ty=y+dy[k];
if(tx<0||ty<0||tx>=n||ty>=m||a[tx][ty]=='0')
continue;
join(tx*m+ty,x*m+y);
}
if(findf(s)==findf(e))
flag=i;
}
cout<<flag<<endl;
}
return 0;
}
添加两点,一点和第一列所有点合并,另一点和最后一列所有点合并,如果这两点在一个集合内了,说明两列内有点在一个集合;
hdu 5652(并查集)的更多相关文章
- 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 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- .NET 通过entity framework报数据库连接错误:ORA-01017: invalid username/password; logon denied
如题,答案为:[ORA-01017].NET程序连接数据库失败 转载文章内容如下: 遇到问题 使用 C#/.NET 开发的一个客户端程序,需要连接 ORACLE 数据库,使用 Oracle.Manag ...
- Spring Cloud 分布式链路跟踪 Sleuth + Zipkin + Elasticsearch【Finchley 版】
随着业务越来越复杂,系统也随之进行各种拆分,特别是随着微服务架构的兴起,看似一个简单的应用,后台可能很多服务在支撑:一个请求可能需要多个服务的调用:当请求迟缓或不可用时,无法得知是哪个微服务引起的,这 ...
- Python3 与 NetCore 基础语法对比(就当Python和C#基础的普及吧)
Jupyter排版:https://www.cnblogs.com/dotnetcrazy/p/9102030.html 汇总系列:https://www.cnblogs.com/dunitian/p ...
- ubuntu 添加开机启动服务
新建umpserver.service [Unit] Description=UMPServer After=syslog.target network.target remote-fs.target ...
- Leetcode 686 Repeated String Match
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- 使用 Travis CI 实现项目的持续测试反馈
[篇幅较长,10.15前补充完毕,如希望探索可直接移步Github仓库:https://github.com/SivilTaram/CITest] 在编程课中,我们可以使用成熟的在线评测系统来测试某个 ...
- Python_列表初识及操作
列表 列表初识 列表属于容器性数据类型. li = [123, '狗', True, [1, 2, 3], {'name': 'Lon'}, (1, 2, 3)] 其中包含有int型,例如123:字符 ...
- C# foreach内部原理
我们知道使用foreach的一个要求是对象必须继承自IEnumerable接口 这样才可以进行迭代 那内部是怎么实现的呢 这个时候会将对应的foreach语句转换为一个while循环 并且通过Move ...
- React Native之code-push的热更新(ios android)
React Native之code-push的热更新(ios android) React Native支持大家用React Native技术开发APP,并打包生成一个APP.在动态更新方面React ...
- Linux 查找文件命令 find whereis locate
Linux 有三个查找文件的命令:find, whereis, locate 其中find 不常用,whereis与locate经常使用,因为find命令速度较慢,因为whereis与locate是利 ...