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. 【Java 线程的深入研究1】Java 提供了三种创建线程的方法

    Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...

  2. 系统管理员应该知道的20条Linux命令

    如果您的应用程序不工作,或者您希望在寻找更多信息,这 20 个命令将派上用场. 在这个全新的工具和多样化的开发环境井喷的大环境下,任何开发者和工程师都有必要学习一些基本的系统管理命令.特定的命令和工具 ...

  3. jQuery-理解选择结果

    使用$函数选择元素返回的是一个对象,可以称这个对象为jQuery对象,在jQuery执行一些操作的时候很多情况下都是返回的jQuery对象. jQuery中常用处理结果集的方法和属性 表达式 例子 说 ...

  4. 如何在ChemDraw中绘制分子立体结构

    ChemDraw是当前最常用的的化学结构绘图软件,软件功能包括化学作图.分子模型生成.化学数据库信息管理等,可以说是化学家和生物学家所需要最终极的化学结构绘图工具.本教程主要介绍ChemDraw绘制分 ...

  5. C语言中FILE是结构体,文件类型的指针

    c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...

  6. mysqldump备份时,--master-data选项的作用是什么?

    需求描述: 今天在研究mysql的备份和恢复,使用mysqldump备份数据库时,用到--master-data选项, 在此,测试并记录选项的作用 测试过程: 1.不使用--master-data进行 ...

  7. MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测

    ->use Admin         (切换到创建用户) ->db.TestDb          (创建数据库) ->db.addUser(“userName”,”Pwd”)   ...

  8. NHibernate初学二之简单执行SQL及HQL、Linq

    上篇文章简单介绍NHibernate之简单增删改查,本文将会简单介绍有关执行NHibernate的SQL.HQL及存储过程: 一:执行SQL语句实例,运用CreateSQLQuery方法 public ...

  9. C# CRC16 查表法

    private static ushort[] crctab = new ushort[256]{                      0x0000, 0x1021, 0x2042, 0x306 ...

  10. Java 实现选择排序

    选择排序: 原理:依次从数组最左边取一个元素,与之后的位置上的元素比較,假设大于/小于(取决于须要升序排还是降序排).则保存较大/较小元素的索引 当一轮比較后,将保存的较大/较小元素的索引与 这轮開始 ...