题目链接: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. openstack 镜像自动扩容 resize拉伸

    The simplest way to support this in your image is to install the cloud-utils package (contains the g ...

  2. hdoj 3572 Task Schedule【建立超级源点超级汇点】

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 第二步 在D2RQ平台上配置jena环境

    第二步 在D2RQ平台上配置jena环境 2013年10月16日 9:48:53 搞了这么长时间语义,只用过protege这样的工具,一直没有落实到实际代码上.jena也看过好久了,总认为是hp公司的 ...

  4. MSSQLSERVER数据库- 字符串分割函数返回类型表

    遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...

  5. 解决错误提示: 未找到 Oracle 客户端和网络组件。

    问题描述: 环境:winxp运行程序连接远程Oracle数据库时报错:未找到 Oracle 客户端和网络组件.这些组件是由 Oracle 公司提供的,是 Oracle 8i 版 (或更高) 客户软件安 ...

  6. c# 可以有多个Main()函数

    可以有多个Main()函数,这样写:namespace ConsoleApp1{class Program{static void Main(string[] args){Console.WriteL ...

  7. ODB 短板

    首先是不支持联合主键,而且没有CHECK约束.UNIQUE约束,这是官方都承认的. 虽然提供了延迟加载(lazyload)来改善效率,不过使用起来不方便 查询不灵活,只能在WHERE之后的部分设定查询 ...

  8. .net+easyui--combobox

    一:预定义结构的 select 元素创建组合框(combobox)值固定写死 <select class="easyui-combobox" name="state ...

  9. Asp.Net Core简单整理

    1.Asp.NetCore 中文入门文档 http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-index.html

  10. 【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】

    错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的 ...