特别经典的一个题,还有一种方法就是二分+bfs

题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走。接着经过q年,每年一个坐标表示此点不能走。问哪年开始图上不能出现最上边不能到达最下边的情况了

图上连通性可以使用并查集判断,但是并查集不善于删边,却善于添边。所以我们倒着来想就是离线倒序添边(横向并查,再纵向并查),当某次判断时图已经连通,就结束。

我使用二维并查集,其实就是使用结构体代替一维数组。接着就是每次一定要从x轴小的点到达x轴大的点,最后注意添边时,我们需要此点向四个方向判断添边

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int xx,yy;
} fat[Max][Max]; //二维并查集
int xx1[Max*Max],yy1[Max*Max];
char str[Max][Max];
int dir[][]= {{,},{-,},{,},{,-}}; //四个方向
void Init(int n,int m)
{
for(int i=; i<n; ++i)
{
for(int j=; j<m; ++j)
{
fat[i][j].xx=i;
fat[i][j].yy=j;
}
}
return ;
}
node Find(int x,int y)
{
if(x==fat[x][y].xx&&y==fat[x][y].yy)
return fat[x][y];
return fat[x][y]=Find(fat[x][y].xx,fat[x][y].yy);
}
int Union(int xx1,int yy1,int xx2,int yy2)//合并两个二维并查集
{
//printf("%d %d %d %d\n",xx1,yy1,xx2,yy2);
node xy1=Find(xx1,yy1);
node xy2=Find(xx2,yy2);
if(xy1.xx==xy2.xx&&xy1.yy==xy2.yy)
return ;
if(xy1.xx<xy2.xx)//保证向下就好
fat[xy1.xx][xy1.yy]=xy2;
else
fat[xy2.xx][xy2.yy]=xy1;
return ;
}
int Jud(int n,int m)//判断是否连通
{
for(int i=; i<m; ++i)
{
node xy1=Find(,i);//第一行可以到达的最下方位置
if(xy1.xx==n-)
return ;
}
return ;
}
int Solve(int n,int m,int q)
{
for(int i=; i<n; ++i)
{
for(int j=; j<m-; ++j)
{
if(str[i][j]==''&&str[i][j+]=='')
{
int ans=Union(i,j,i,j+);//横向合并
//printf("%d\n",ans);
}
}
}
for(int j=; j<m; ++j)
{
for(int i=; i<n-; ++i)
{
if(str[i][j]==''&&str[i+][j]=='')
{
int ans=Union(i,j,i+,j);//纵向合并
//printf("ver=%d\n",ans);
}
}
}
if(Jud(n,m))
return -;
int p;
for(int i=q-; i>=; --i)
{
p=;
str[xx1[i]][yy1[i]]='';
while(p<)
{
if(xx1[i]+dir[p][]>=&&xx1[i]+dir[p][]<n&&yy1[i]+dir[p][]>=&&yy1[i]+dir[p][]<m&&str[xx1[i]+dir[p][]][yy1[i]+dir[p][]]=='')//需要连接四个方向可行的地方
Union(xx1[i],yy1[i],xx1[i]+dir[p][],yy1[i]+dir[p][]);
p++;
}
if(Jud(n,m))
return i+;
}
return ;
}
int main()
{
int t,n,m,q;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
Init(n,m);
for(int i=; i<n; ++i)
scanf("%s",str[i]);
scanf("%d",&q);
for(int i=; i<q; ++i) //存下来,倒着增加边
{
scanf("%d %d",&xx1[i],&yy1[i]);
str[xx1[i]][yy1[i]]='';
}
printf("%d\n",Solve(n,m,q));
}
return ;
}

HDU 5652 India and China Origins(经典并查集)的更多相关文章

  1. HDU 5652 India and China Origins 二分+并查集

    India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...

  2. HDU 5652 India and China Origins(并查集)

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. 并查集(逆序处理):HDU 5652 India and China Origins

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. hdu 5652 India and China Origins 并查集+二分

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  6. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...

  7. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

  8. hdu5652:India and China Origins(并查集)

    倒序操作用并查集判断是否连通,新技能get√(其实以前就会了 这题细节很多...搞得整个程序都是调试输出,几度看不下去想要重写 并查集到现在大概掌握了两个基本用途:判断是否连通 / 路径压缩(上一篇b ...

  9. hdu 5652 India and China Origins 二分+bfs

    题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...

随机推荐

  1. c# 获取系统版本,获取net framework 版本(Environment 类)

    1.获取当前操作系统版本信息 使用Environment.OSVersion 属性 获取包含当前平台标识符和版本号的 OperatingSystem 对象. 命名空间:  System程序集:  ms ...

  2. pdf.js使用教程

    pdf.js框架的魅力所在,为其为HTML5实现的,无需任何本地支持,而且对浏览器的兼容性也是比较好,要求只有一个:浏览器支持HTML5就好了!(不过对于低版本的IE,就只能节哀了!) 据说IE9以上 ...

  3. 完美解决:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x

    如题: 原因:没有配置resolv.conf 解决方法: 到/etc目录下配置resolv.conf加入nameserver IP,如: nameserver 8.8.8.8nameserver 8. ...

  4. 【XLL API 函数】xlfUnregister (Form 1)

    此函数可以被 Excel 已经载入的 XLL 或 DLL 调用.它等效于宏表函数 UNREGISTER. xlfUnregister 有两种调用形式: 形式1:Unregister 单独的命令或函数 ...

  5. Quartz结合SPRING多任务定时调用

    定义两个被调度的类 public class QuartzJob { public void work() { System.out.println(Spring Quartz的任务调度1被调用!&q ...

  6. July 5th, Week 28th Tuesday, 2016

    If you smile when no one else is around, you really mean it. 独处的时候你的笑容才是发自内心的笑容. Human beings are so ...

  7. grep(Global Regular Expression Print)

    .grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...

  8. 解决eclipse中maven web工程打包成war(发布到tomcar)时lib中没有jar包的解决方法

    可能有两个原因:1.maven中某些jar包下载不下来 从其他地方下载jar文件放到相应maven本地库的.m2里2..classpath文件中缺少(下面代码的作用是制定maven的jar发布路径)& ...

  9. A Horrible Poem(bzoj 2795)

    Description 给出一个由小写英文字母组成的字符串S,再给出q个询问,要求回答S某个子串的最短循环节.如果字符串B是字符串A的循环节,那么A可以由B重复若干次得到. Input 第一行一个正整 ...

  10. tomcat和apache区别联系

    tomcat和apache区别联系 Apache是普通服务器,本身只支持html即普通网页.不过可以通过插件支持php,还可以与Tomcat连通(单向Apache连接Tomcat, 就是说通过Apac ...