India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1446 Accepted Submission(s): 495
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 4 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 0,1 characters. 1 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≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X<N
0≤Y<M
Output
Single line at which year the communication got cut off.
print -1 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
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
Sample Output
4
Source
题目大意:Bestcoder Round #77(div2)里边有中文题意,这里就不重复说明了。
思路:
1、正常思路:对于Q个添加山峰的操作,每添加一次山峰,就要跑一遍Bfs判连通与否,如果还是连通的。然而其时间复杂度为:O(Q*(n*m))是一个非常巨大的时间耗费量,显然会T
2、优化思路:对于Q个添加山峰的操作,对于关键一次添加山峰之后如果不连通了,那么之后的山峰添加上之后也是不连通的,所以满足二分查找的单调递增性,我们可以使用二分查找来降低Q的时间复杂度,优化后其时间复杂度为:O(logQ*(n*m)),显然优化之后,就不会T了。
3、对于Bfs的部分:拿上边为0的节点作为起点,跑Bfs,如果能够有情况跑到下边界,那么就说明当前情况是连通的,否则不连通。
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zuobiao
{
int x,y;
}now,nex;
int n,m,q;
int dian[505*505][2];
int vis[505][505];
char a[505][505];
char b[505][505];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int Bfs()
{
queue<zuobiao >s;
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
if(a[0][i]=='0')
{
now.x=0;
now.y=i;
s.push(now);
vis[now.x][now.y]=1;
}
}
while(!s.empty())
{
now=s.front();
if(now.x==n-1)return 1;
s.pop();
for(int i=0;i<4;i++)
{
nex.x=now.x+fx[i];
nex.y=now.y+fy[i];
if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&a[nex.x][nex.y]=='0'&&vis[nex.x][nex.y]==0)
{
vis[nex.x][nex.y]=1;
s.push(nex);
}
}
}
return 0;
}
int Slove(int mid)
{
for(int i=0;i<=mid;i++)
{
a[dian[i][0]][dian[i][1]]='1';
}
int tt=Bfs();
for(int i=0;i<=mid;i++)
{
a[dian[i][0]][dian[i][1]]='0';
}
return tt;
}
void erfen()
{
int l=0;
int r=q-1;
while(r-l>=0)
{
int mid=(l+r)/2;
if(Slove(mid)==1)
{
l=mid+1;
}
else
{
r=mid-1;
}
}
if(l+1>q)printf("-1\n");
else
printf("%d\n",l+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d%d",&dian[i][0],&dian[i][1]);
}
erfen();
}
}