India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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
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.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int l,k;
char a[][];
struct is
{
int l,r;
}ch[];
int father[];
int vis[][];
int xz[]={,,-,};
int yz[]={,,,-};
int find_father(int x)
{
return x==father[x]? x:father[x]=find_father(father[x]);
}
void hebing(int x,int y)
{
int xx=find_father(x);
int yy=find_father(y);
if(xx!=yy)
father[xx]=yy;
}
int number(int x,int y)
{
return (x-)*k+y;
}
int check(int ff)
{
memset(vis,,sizeof(vis));
for(int i=;i<=l*k;i++)
father[i]=i;
father[]=;
father[]=;
for(int i=;i<=l;i++)
for(int t=;t<=k;t++)
if(a[i][t]=='')
vis[i][t]=;
else
vis[i][t]=;
for(int i=;i<=ff;i++)
vis[ch[i].l+][ch[i].r+]=;
for(int i=;i<=l;i++)
for(int t=;t<=k;t++)
{
if(vis[i][t])continue;
for(int j=;j<;j++)
{
int xx=i+xz[j];
int yy=t+yz[j];
if(yy<=||yy>k||vis[xx][yy])continue;
if(xx==)hebing(number(i,t),);
else if(xx==l+)hebing(number(i,t),);
else hebing(number(i,t),number(xx,yy));
}
}
int ans1=find_father();
int ans2=find_father();
if(ans1==ans2)
return ;
return ;
}
int main()
{
int x,y,i,t;
scanf("%d",&x);
while(x--)
{
scanf("%d%d",&l,&k);
for(i=;i<=l;i++)
scanf("%s",a[i]+);
scanf("%d",&y);
for(t=;t<=y;t++)
scanf("%d%d",&ch[t].l,&ch[t].r);
if(!check())
{
printf("-1\n");
continue;
}
int st=,en=y,mid;
while(st<en)
{
mid=(st+en)>>;
if(check(mid))
st=mid+;
else
en=mid;
}
printf("%d\n",st);
}
return ;
}
代码