题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652

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 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 , characters. 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≤ ≤N≤ ≤M≤ ≤Q≤N∗M ≤X<N ≤Y<M Output
Single line at which year the communication got cut off. print - 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 Sample Output

题意:中国和印度中间隔着平原个高山,人们只能走平原,高山翻不过去,每年会有一个位置的平原变成高山,问第几年开始,人们不能来往了,如果都能来往输出-1

方法:用二分搜第几年开始不能来往,用dfs判断是否能来往

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a));
const int oo = 0x3f3f3f3f;
const int N = ;
char str[N][N],maps[N][N];
int dis[][]={{,},{,},{-,},{,-}};
int x[N*N],y[N*N];
int vis[N][N];
int n,m;
struct node
{
int x,y;
};
int dfs(int x,int y)
{
met(vis,);
queue<node> Q;
node q,p;
q.x=x;q.y=y;
Q.push(q);
vis[x][y]=;
while(Q.size())
{
q=Q.front();
Q.pop();
if(q.x==n-)
return ;
for(int i=;i<;i++)
{
p.x=q.x+dis[i][];
p.y=q.y+dis[i][];
if(p.x>= && p.x<n && p.y>= && p.y<m && !vis[p.x][p.y]&& maps[p.x][p.y]=='')
{
vis[p.x][p.y]=;
Q.push(p);
}
}
}
return ;
}
int pan()
{
for(int i=;i<m;i++)
{
if(maps[][i]=='')
{
if(dfs(,i))
return ;
}
}
return ;
}
void buile(int mid)
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
maps[i][j]=str[i][j];
}
for(int i=;i<=mid;i++)
maps[x[i]][y[i]]='';
}
int main()
{
int t,q;
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",&x[i],&y[i]);
int l=,r=q;
int mid=;
while(l<=r)///二分查找查到那一年开始不通的
{
met(maps,);///建立新的地图
mid=(l+r)/;
buile(mid);
if(!pan())///如果不通才往前找一年
{
r=mid-;
}
else ///否则往后找一年
l=mid+;
}
if(l>q)///如果找到的那年比q大,说明一直是通的输出-1
l=-;
printf("%d\n",l);
}
return ;
}

(hdu)5652 India and China Origins 二分+dfs的更多相关文章

  1. HDU 5652 India and China Origins 二分+并查集

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

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

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

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

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

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

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

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

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

  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表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

随机推荐

  1. 【解决】Django下使用sqlite3的相关问题

    最近在玩Django,想用它写一个很小很小的项目,Django自带数据库sqlite3,本来项目也小,我就用它了. 玩意虽小,东西却不是那么好用的. 首先,在项目中建立模型,一个例子是这样的: cla ...

  2. glusterfs repo

    Installing Gluster For RPM based distributions, if you will be using InfiniBand, add the glusterfs R ...

  3. java中通过反射获取方法并且调用(getMethod和invoke深入)实践

    为了支持业务的快速变更,往往采用可配置的方式,将业务逻辑的处理部分配置在数据库中或者XMl文件里.配置什么,如何配置才更灵活,That's a problem. 以数据库配置为例(xml相同),在数据 ...

  4. TreeMap 排序

    一.TreeMap TreeMap 默认排序规则:按照key的字典顺序来排序(升序) 当然,也可以自定义排序规则:要实现Comparator接口. 用法简单,先看下下面的demo public cla ...

  5. Install Package and Software

    svn http://tortoisesvn.sourceforge.net/ git https://download.tortoisegit.org/ http://git-for-windows ...

  6. 【转】Spring的WebServiceTemplate访问WebService的方法及其本质原理

    WebService客户端调用的本质就是将SAOP格式的XML通过通信协议发送到WebService的服务器端,然后接收服务器端返回的XML. 本文简单介绍一下如何通过Spring提供的WebServ ...

  7. [Javascript] Gradient Fills on the HTML5 Canvas

    window.onload = function() { var canvas = document.getElementById("canvas"), context = can ...

  8. Android官方技术文档翻译——ApplicationId 与 PackageName

    本文译自androd官方技术文档<ApplicationId versus PackageName>,原文地址:http://tools.android.com/tech-docs/new ...

  9. C# - 系统类 - DateTime类

    DateTime类 ns:System 此类是一个结构 提供了访问和修改它所代表的时间 创建DateTime实例的几种方式 DateTime time = , , , , , ); Console.W ...

  10. Iperf使用方法

    Iperf使用方法 Iperf  是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性.Iperf可以报告带宽,延迟抖动和数据包 ...