hdu-5652 India and China Origins(二分+bfs判断连通)

时间:2022-04-21 12:37:22

题目链接:

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.

hdu-5652 India and China Origins(二分+bfs判断连通)

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:

hdu-5652 India and China Origins(二分+bfs判断连通)

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 二分&plus;bfs

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

  2. HDU 5652 India and China Origins 二分&plus;并查集

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

  3. &lpar;hdu&rpar;5652 India and China Origins 二分&plus;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 二分优化&plus;BFS剪枝

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

  5. hdu 5652 India and China Origins 并查集&plus;二分

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

  6. 并查集&lpar;逆序处理&rpar;: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(二分&plus;bfs &vert;&vert; 并查集)BestCoder Round &num;77 &lpar;div&period;2&rpar;

    题意: 给一个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. Android的onMeasure和onLayout And MeasureSpec揭秘

    Android中自定义ViewGroup最重要的就是onMeasure和onLayout方法,都需要重写这两个方法,ViewGroup绘制 的过程是这样的:onMeasure → onLayout → ...

  2. ACM题目————Find them&comma; Catch them

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  3. C&num; DataGridView的列对象属性探讨 &lpar;未完待续&rpar;

    比较难的几个属性的释义[1]:

  4. Vue应用框架整合与实战--Vue技术生态圈篇

    实用框架以及工具 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 Element-UI ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 ...

  5. 算法笔记--manacher算法

    参考:https://www.cnblogs.com/grandyang/p/4475985.html#undefined 模板: ; int p[N]; string manacher(string ...

  6. 在Qt项目中添加全局宏变量来达到按方案编译的目的

    遇到一个需求,需要根据不同需要编译所需程序,本人采用了在QtCreator中建立不同的构建设置,配合宏的方式来实现: 1.在项目-构建设置中,添加构建配置2.在构建步骤里的qmake中的额外参数一栏填 ...

  7. 关于CALayer导致的crash问题

    push到一个页面进行绘图时,设置如下: CALayer * layer = [CALayer layer]; layer.frame = CGRectMake(, , , ); layer.dele ...

  8. Google protobuf解析消息逻辑的版本问题

    在分析caffe2源码的过程中,由于caffe2使用protobuf作为网络结构和网络参数序列化和反序列化的机制,想在反序列化之前进行加解密处理,这是反向protouf其实有两个版本的实现来进行消息的 ...

  9. org&period;I0Itec&period;zkclient&period;exception&period;ZkTimeoutException&colon; Unable to connect to zookeeper server within

    org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeo ...

  10. GDI的 点 线 面 双缓冲 位图的绘制

    1.输出文本 // 输出文本 ,,)); //设置字体颜色,但最后都要返回原来的字体格式 COLORREF clrBackColor = SetBkColor(hDC, RGB(,,)); //设置背 ...