India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 441    Accepted Submission(s): 133

Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.








Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people
can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between
two white portions you can't travel by climbing the mountain.

And at each step people can go to 4 adjacent positions.



Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
 
Input
There are multi test cases. the first line is a sinle integer T which
represents the number of test cases.

For each test case, the first line contains two space seperated integers N,M.
next N lines
consists of strings composed of 0,1 characters. 1 denoting
that there's already a mountain at that place, 0 denoting
the plateau. on N+2 line
there will be an integer Q denoting
the number of mountains that rised up in the order of times. Next Q lines
contain 2 space
seperated integers X,Y denoting
that at ith year a mountain rised up at location X,Y.



T≤10



1≤N≤500



1≤M≤500



1≤Q≤N∗M



0≤X<N



0≤Y<M
 
Output
Single line at which year the communication got cut off.



print -1 if these two countries still connected in the end.



Hint:








From the picture above, we can see that China and India have no communication since 4th year.
 
Sample Input
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
 
Sample Output
4
 
Source

BestCoder Round #77 (div.2)

用二分加验证可以过,用并查集也可以过。

这个是并查集,

</pre><p style="height:auto; margin:0px; padding:0px 20px; font-size:14px; font-family:'Times New Roman'"><pre name="code" class="html">#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 250000
int father[MAX+5];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
int b[MAX][2];
char a[505][505];
int c[505][505];
int q;
int n,m; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",a[i]+1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&b[i][0],&b[i][1]);
b[i][0]++;b[i][1]++;
a[b[i][0]][b[i][1]]='1';
}
for(int i=0;i<=n*m+1;i++)
father[i]=i;
for(int i=1;i<=m;i++)
{
if(a[1][i]=='0') father[i]=0;
if(a[n][i]=='0') father[(n-1)*m+i]=n*m+1;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='1')
continue;
if(a[i][j-1]=='0'&&j!=1)
{
int fx=find((i-1)*m+j);
int fy=find((i-1)*m+j-1);
if(fx!=fy)
father[fx]=fy;
}
if(a[i-1][j]=='0')
{
int fx=find((i-1)*m+j);
int fy=find((i-2)*m+j);
if(fx!=fy)
father[fx]=fy;
}
}
} if(find(0)==find(n*m-1))
{
printf("-1\n");
continue;
}
int i;
for( i=q;i>=1;i--)
{
for(int j=0;j<4;j++)
{
int x=b[i][0],y=b[i][1];
if(x==1)
father[(x-1)*m+y]=0;
if(x==n)
father[(x-1)*m+y]=n*m+1;
int xx=x+dir[j][0];int yy=y+dir[j][1];
if(xx<1||xx>n||yy<1||yy>m||a[xx][yy]=='1')
continue;
int fx=find((x-1)*m+y);
int fy=find((xx-1)*m+yy);
if(fx!=fy)
father[fx]=fy;
}
a[b[i][0]][b[i][1]]='0';
if(find(0)==find(n*m+1))
break;
}
printf("%d\n",i);
}
return 0;
}

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

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

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

  2. hdu 5652 India and China Origins 并查集

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

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

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

  4. hdu5652 India and China Origins(并查集)

    India and China Origins  Accepts: 49  Submissions: 426  Time Limit: 2000/2000 MS (Java/Others)  Memo ...

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

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

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

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

  7. HDU 5652 India and China Origins(经典并查集)

    特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...

  8. hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)

    题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...

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

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

随机推荐

  1. repo manifest.xml 分析

    repo是用于管理android的git仓库的工具. 之前想将android的代码放在github上面,并通过repo进行管理.但一直不知道怎么添加进去,那么多的git仓库,难道都要手动建立吗? 直到 ...

  2. linux gzip 命令详解

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip  ...

  3. CentOS开关机命令

    命令简介 shutdown,poweroff,reboot,halt,init都可以进行关机,大致用法. /sbin/halt     [-n] [-w] [-d] [-f] [-i] [-p] [- ...

  4. 【Java面试题】29 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。

    本题并不难,实现方式有很多种,有很多种结构. 方法一:利用内部类实现,两个实现加减的类实现Runnable接口,然后再实现4个具体线程. 代码: public class ManyThreads { ...

  5. CleanMyMac 4破解版-最强中文版_破解版_激活码_注册码

    最新版CleanMyMac 4中文版本已经发布了,也受到了广大用户的喜爱.众所周知, 注册码是开启软件的钥匙,在获取软件安装包之后需要有效的注册码才能激活软件.但是关于CleanMyMac 4注册码的 ...

  6. php对gzip的使用(理论)

    gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992年10月31日第一 ...

  7. ASP.NET的用户控件

    本文介绍如何在ASP.NET中创建用户控件,控件属性的动态修改以及控件的事件出发机制. 简介ASP.NET的服务端控件使得Web开发工作变得更为简单,功能更为强大.我们介绍过如何在ASP.NET页面中 ...

  8. 【转】理清基本的git(github)流程

    概述 当我初次接触git时,我需要快速学习基本的git工作流,以便快速接收一个开源Web项目维护.但是,我很难理解工作流程,因为我不太了解git使用关键点. fork,clone,pull.branc ...

  9. [ML] I'm back for Machine Learning

    Hi, Long time no see. Briefly, I plan to step into this new area, data analysis. In the past few yea ...

  10. swift - UIScrollView 的使用

    本节详细介绍scrollview的用法 ———————————————————————————————————— UIScrollView 是一个能够滚动的视图控件,可以用来展示大量的内容,并且可以通 ...