题目链接:

India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 685    Accepted Submission(s): 230

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
 
 
 
题意:    
 
给你一些0和1组成的字符串,表示i行j列的状态,0表示平原,1表示山峰,然后再给你q个数对表示在第i年位置l,r处要变成山峰;问最早在第几年印度和中国之间不再连通了;
 
 
思路:
 
比赛的时候直接bfs判断连通性,最后tle了,后来看别人博客说要二分,或者用并查集,想了一会还是想不好并查集怎么处理,就写了个二分+bfs;啊啊,写了几个二分之后发现现在写二分好好玩啊,一开始入ACM坑的时候写的二分都忒傻逼的那种,现在终于变成熟了,啊哈哈哈哈;我骄傲!不过现在我仍然好弱好弱,还是要不断加油才行;
 
 
 
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,q,dir[][]={,,,-,,,-,},vis[][],l[],r[];
char str[][];
struct node
{
int x,y;
};
node a;
queue<node>qu;
int check(int num)
{
while(!qu.empty())qu.pop();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='')vis[i][j]=;
else vis[i][j]=;
}
}
for(int i=;i<=num;i++)
{
vis[l[i]][r[i]]=;
}
for(int k=;k<m;k++)
{
if(!vis[][k])
{
a.x=;
a.y=k;
qu.push(a);
}
while(!qu.empty())
{
int topx=qu.front().x,topy=qu.front().y;
qu.pop();
if(topx==n-)return ;
for(int i=;i<;i++)
{
int fx=topx+dir[i][],fy=topy+dir[i][];
if(fx>=&&fx<n&&fy>=&&fy<m)
{
if(!vis[fx][fy])
{
if(fx==n-)return ;
a.x=fx;
a.y=fy;
qu.push(a);
vis[fx][fy]=;
}
}
}
}
}
return ;
}
int bis()
{
int L=,R=q,mid;
while(L<=R)
{
mid=(L+R)>>;
if(check(mid))L=mid+;
else R=mid-;
}
if(L>q)return -;
return L;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
}
scanf("%d",&q);
for(int i=;i<=q;i++)
{
scanf("%d%d",&l[i],&r[i]);
}
printf("%d\n",bis());
}
return ;
}

hdu-5652 India and China Origins(二分+bfs判断连通)的更多相关文章

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

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

  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 二分+dfs

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

  4. HDU 5652 India and China Origins 二分优化+BFS剪枝

    题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...

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

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

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

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

  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 并查集

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

随机推荐

  1. JQuery基础知识--方便忘记时查看

    第一次写博客,不多说废话,实用为先.如有不对,请多指正. JQuery api 第一步引入JQuery库. <script type="text/javascript" sr ...

  2. Lumen开发:lumen源码解读之初始化(5)——注册(register)与启动(boot)

    版权声明:本文为博主原创文章,未经博主允许不得转载. register()是在服务容器注册服务, bootstrap/app.php /** * 注册外部服务 */ $app->register ...

  3. thinkphp自动验证无效的问题

    新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...

  4. J - 组合

    J - 组合 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description 有两 ...

  5. 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积

    http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...

  6. Qt状态机框架(状态机就开始异步的运行了,也就是说,它成为了我们应用程序事件循环的一部分了)

    状态机框架 Qt中的状态机框架为我们提供了很多的API和类,使我们能更容易的在自己的应用程序中集成状态动画.这个框架是和Qt的元对象系统机密结合在一起的.比如,各个状态之间的转换是通过信号触发的,状态 ...

  7. [luogu4255]公主の#18文明游戏

    [luogu4255]公主の#18文明游戏 luogu 发现没有连边,只有删边? 考虑倒着做 开map记M[i][j]表示编号为i的并查集,信仰j的人数 s[i]表示编号为i的并查集的总人数 首先询问 ...

  8. 提供的STC89C52RC单片机GPS在LCD1602液晶只显示welcome to use问题?

    1.使用USB-TTL接GPS单独测试GPS定位在所处环境看是否能定位到. 2.检查自己使用的单片机是不是STC89C52RC型号,提供例程使用的这个型号单片机.如果使用其他51单片机,请先使用STC ...

  9. java并发实现原子操作

    来自<java并发编程的艺术>.只是方便自己以后查找. 处理器如何实现原子操作 32位IA-32处理器使用基于对缓存加锁或总线加锁的方式来实现多处理器之间的原子操作.首先处理器会自动保证基 ...

  10. curl 监控web

    [root@rhel6 ~]# curl -I -s -w "%{http_code}\n" -o /dev/null http://127.0.0.1 [root@rhel6 ~ ...