India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 676    Accepted Submission(s): 227

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
  这题感觉怎么搞都可以,二分也可以AC,我用的是官方题解提到的并查集。
  由于并查集不便于分开集合,应当考虑逆序处理。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int fa[maxn];
bool out[maxn];
int X[maxn],Y[maxn];
int map[][];
char s[];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} int main(){
int T,R,C,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&R,&C);
for(int i=C+;i<=(R+)*C;i++)fa[i]=i;
for(int i=;i<=C;i++)fa[i]=;
for(int i=C*(R+)+;i<=C*(R+);i++)fa[i]=C*(R+)+; for(int i=;i<=R;i++){
scanf("%s",s+);
for(int j=;j<=C;j++)
map[i][j]=s[j]-'';
} scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&X[i],&Y[i]);X[i]++;Y[i]++;
map[X[i]][Y[i]]=;
} for(int i=;i<=R;i++)
for(int j=;j<C;j++){
if(map[i][j])continue;
if(map[i-][j]==)fa[find((i-)*C+j)]=find(i*C+j);
if(map[i+][j]==)fa[find((i+)*C+j)]=find(i*C+j);
if(map[i][j+]==)fa[find(i*C+j+)]=find(i*C+j);
if(map[i][j-]==)fa[find(i*C+j-)]=find(i*C+j);
} int ans=;
for(int i=m;i>=;i--){
if(find()==find(C*(R+)+)){
ans=i+;
break;
}
map[X[i]][Y[i]]=;
if(map[X[i]-][Y[i]]==)fa[find((X[i]-)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]+][Y[i]]==)fa[find((X[i]+)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]+]==)fa[find(X[i]*C+Y[i]+)]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]-]==)fa[find(X[i]*C+Y[i]-)]=find(X[i]*C+Y[i]);
}
if(ans==m+)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}

并查集(逆序处理):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 二分+并查集

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

  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 并查集+逆序

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

  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(经典并查集)

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

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

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

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

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

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

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

随机推荐

  1. js 高阶函数 filter

    filter用于过滤array中的一些值,通过带入的函数返回的ture 或false 保留或去除,返回一个新的array filter 使用演示:判断筛选出array中的素数: //判断素数自定义函数 ...

  2. widows sever2003 PHP环境搭建

    此文仅为文字笔记,非原创,摘阅自互联网  1.安装IIS6.0及Framework 2.0  2.安装fastcgi http://www.iis.net/download/FastCGI 安装fas ...

  3. 我的C# - Web - DAL- DBHelper.cs

    其中的部分内容是别人的,我修改过了并加入了详细的注释!!! 一.这个DBHelper的大致块儿如下图: 二.下面是具体的源代码: //命名空间..... using System;using Syst ...

  4. Jquery不生效

    $(document).ready(function(){这个都没有生效, 1.网上查了说是jquery的路劲引入的有问题,经查并不是这个问题 2.换了一个jquery的版本,发现生效了. 原不生效文 ...

  5. Asp.net MVC利用Ajax.BeginForm实现bootstrap模态框弹出,并进行前段验证

    1.新建Controller public ActionResult Index() { return View(); } public ActionResult Person(int? id) { ...

  6. Xcode添加静态库以及编译选项配置常见问题

    一,Xcode编译出现Link错误,出现"duplicate symbols for architecture i386 clang"提示.问题:链接时,项目有重名文件.解决:根据 ...

  7. [转]mysql 导入导出数据库以及函数、存储过程的介绍

    本篇文章是对mysql中的导入导出数据库命令以及函数.存储过程进行了详细的分析介绍,需要的朋友参考下: mysql常用导出数据命令:1.mysql导出整个数据库  mysqldump -hhostna ...

  8. SGU 113.Nearly prime numbers

    水一个代码: #include <iostream> using namespace std; int n, a; bool ok; bool prime (int x) { ; i * ...

  9. SGU 249.Matrix(Gray码)

    题意: 用0到2^(n+m-1)这2^(n+m-1)个数填在一个2^n*2^m的矩阵里,使得所有相邻的数的二进制表示只有一位不同. Solution: Gray码.对于第i行第j列的数,由i的Gray ...

  10. BeanUtils在web项目中的应用

    package cn.gdpe.jdbc; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; im ...