HDU 5652 India and China Origins(并查集)
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

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.
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
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.
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
4
用二分加验证可以过,用并查集也可以过。
这个是并查集,
</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(并查集)的更多相关文章
- hdu 5652 India and China Origins 并查集+二分
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- hdu5652 India and China Origins(并查集)
India and China Origins Accepts: 49 Submissions: 426 Time Limit: 2000/2000 MS (Java/Others) Memo ...
- HDU 5652 India and China Origins 二分+并查集
India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...
- 并查集(逆序处理):HDU 5652 India and China Origins
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
随机推荐
- 【转】【Android】Android不同版本下Notification创建方法
使用 new Notification(int icon, CharSequence tickerText, long when)构造函数时,Eclipse却提示:" The constru ...
- 【转】DWM 窗体玻璃效果实现
我一直盼望着 Windows 新版本的发布.令人感兴趣的事情莫过于浏览 MSDN® 和 SDK 文档,查找一些可以利用和依赖的最新创新,然后让朋友和同事以及您的老板(如果幸运的话)大开眼界.Windo ...
- 在Access中执行SQL语句
Access在小型系统开发中等到了广泛使用.虽然Access提供了可视化的操作方法,但许多开发人员还是喜欢直接用SQL语句操作数据表.如何在Access中打开SQL视图,对于初次使用Access的程序 ...
- linux基础知识的总结
例如以下内容是我对linux基础知识的总结,由于本人在初期学习linux的时候走了不少的弯路,对于基础的掌握耗费了不少的时间,所以为了后来者对linux的基础部分有个清晰的了解,特对基础知识进行了总结 ...
- PL/SQL developer(绿色版)安装及配置
1.PL/SQL Developer下载地址:百度网盘: 2.tsname.ora配置: orcl = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS )) ) (CO ...
- c++ ++i and i++
++i 在 i 存储的值上增加一并向使用它的表达式 ``返回" 新的, 增加后的值; 而 i++ 对 i 增加一, 但返回原来的是未增加的值. 至于++i和i++有什么区别,举个例子 1.a ...
- js 文件下载
工程WebApi: 点击按钮执行的handler exportClick() { var profile = { content: this.state.profile, type: MappingT ...
- 【scrapy】相关
http://www.cnblogs.com/mophee/archive/2009/03/12/1409562.html css选择器中的空格 http://www.crummy.com/softw ...
- C语言中FILE是结构体,文件类型的指针
c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...
- java jdk-awt.font在centos上中文乱码的问题, 安装中文字体
有需求生成一个二维码,并且有一段文本说明,但是使用awt.font来生成中文时,一直存在乱码的问题.网上的解决办法有几种,但是在centos上亲测有用的就是如下的方法. Java代码如下:new ja ...