(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 ;
}
上一篇:objective-c 遍历文件夹查看文件


下一篇:c#工厂模式与抽象工厂模式