hdu-5652 India and China Origins(二分+bfs判断连通)
题目链接:
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
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.
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.
#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判断连通)的更多相关文章
- hdu 5652 India and China Origins 二分+bfs
题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...
- 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 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- 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
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- 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 || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
随机推荐
- PHP-Manual的学习----【语言参考】----【类型】-----【Resource 资源类型】
2017年8月24日11:29:361.资源 resource 是一种特殊变量,保存了到外部资源的一个引用.资源是通过专门的函数来建立和使用的.2.由于资源类型变量保存有为打开文件.数据库连接.图形画 ...
- iOS 富文本类库RTLabel
本文转载至 http://blog.csdn.net/duxinfeng2010/article/details/9004749 本节关于RTLable基本介绍,原文来自 https://git ...
- ULN2003A 使用,有坑
8脚接24V负极 9脚接24V正极 16接24V继电器,再接到24V正极 1-7无论给5V 正 或 负,10-16都不能达到24V,越靠近输入端的输出端电压越大,最大的才11V,最小的2.5V 最后发 ...
- 九度OJ 1195:最长&最短文本 (搜索)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3144 解决:1156 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输 ...
- nginx 基础配置详解
#本文只对nginx的最基本配置项做一些解释,对于配置文件拆分管理,更详细的集群健康检查的几种方式,检查策略等在此不做详细解释了. #运行用户user nobody;#启动进程,通常设置成和cpu的数 ...
- Android笔记之使用ZXing扫描二维码
ZXing发布版下载地址:https://github.com/zxing/zxing/releases 为了能让官方Demo跑起来,先把ZXing核心部分core复制到自己的工程里 还要把andro ...
- PBR探索
原理 根据能量守恒,以及一系列光照原理得出微表面BRDF(Bidirectional Reflectance Distribution Function)公式 // D(h) F(v,h) G(l,v ...
- go语言之接口一
在Go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口 我们定义了一个File类,并实现有Read().Write().Seek().Close()等方法.设 想我们有如下接口 ...
- 【转载】解决Apache2+PHP上传文件大小限制的问题
原文出处:http://evol1216.blog.163.com/blog/static/13019958020106783623528/ 在用PHP进行文件上传的操作中,需要知道怎么控制上传文件大 ...
- 如何用excel urldecode解码把url编码转为汉字?
统计分析可以反映出网站运营的情况,并根据实际作出相应的调整,是站长必需的基础技能.ytkah感觉最好用的是谷歌统计,里面有个搜索关键词及对应受访页面,这个功能对优化用处很大,但大家都知道访问不太顺畅. ...