(hdu)5652 India and China Origins 二分+dfs
题目链接: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的更多相关文章
- 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 二分+bfs
题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...
- 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 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- 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表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
随机推荐
- spring 的配置 bean>>property>>name属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- HW1.7
public class Solution { public static void main(String[] args) { System.out.println("π = " ...
- <转>Windows 各种计时函数总结
本文转自MoreWindows 特此标识感谢 http://blog.csdn.net/morewindows/article/details/6854764 本文对Windows平台下常用的计时函数 ...
- java使用xheditor Ajax异步上传错误
java使用xheditor Ajax异步上传时候错误如下:the request doesn't contain a multipart/form-data or multipart/mixed s ...
- GridLookUpEdit 简单应用
在属性列表中 Properties -> view 即可打开设计器进行编辑 后台代码: DataTable dtt = stu.StuGetFind(sxml, 1, 50).Tables[ ...
- MongoDB--使用修改器修改文档
可以使用修改器啦修改文档,比如增加.删除文档的键值.使用修改器首先要定位到某个文档, 然后再增加相应的修改选项,需要使用update语句 1.$inc修改器修改文档 > db.users.fin ...
- Dom4j写XML
package com; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.I ...
- Installing the PHP/MongoDB extension on Mac OSX 10.8
Installing PHP/MongoDB extension is a two steps task on OSX: Install the autoconf tool required for ...
- spring scheduler相同时间内执行两次的问题
在网上找了大片文章,有的说是上下文被夹在两次的问题,可我配置scheduler的上下文着实没被夹在多次 之后才在网上找到,虽然还不明白原理,但是还是贴出来分享下
- Java 加密 MD5
版权声明:本文为博主原创文章,未经博主允许不得转载. [md5] md5是一种哈希算法,哈希算法是啥? ... 特点是不能解密. [代码] package com.uikoo9.util.encryp ...