题目链接: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. Kooboo 加Search功能 必须先ReBuild Index Data

      加Search功能   有几个要点 1. 需要在Kooboo 必须先 ReBuild Index Data 2. 需要在要搜索的page中启用搜索索引         搜索的代码 @using K ...

  2. SSDT – Error SQL70001 This statement is not recognized in this context-摘自网络

    March 28, 2013 — arcanecode One of the most common errors I get asked about when using SQL Server Da ...

  3. PHP函数补完:var_export()

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...

  4. light oj 1153 - Internet Bandwidth【网络流无向图】

    1153 - Internet Bandwidth   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  5. Chrome 插件vimium快捷键大全

    vimium是一款让你在chrome浏览器能方便地使用键盘操作浏览器的插件. 转自:http://www.cnblogs.com/liuyangnuts/p/3474905.html 在当前页中导航 ...

  6. 增强Eclipse ,MyEclipse 的代码自动提示功能

    一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项是默认关闭的, ...

  7. crm操作权限

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using System.Colle ...

  8. VOA学习-South Sudan Must Allow Aid

    South Sudan Must Allow Aid The United States is gravelyconcerned by the serious escalation of the hu ...

  9. leecode 每日解题思路 152 Maximun Product Subarray

    问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问 ...

  10. 如何去除CISCO交换机中的口令??

    加电后按住交换机前面的那个按钮 灯不闪了以后松手这时交换机会进入switch:模式输入命令 flash然后 dir flash:你会发现有个 config.text 的文件 你的密码和配置都保存在那里 ...