India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 118 Accepted Submission(s): 34
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.
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
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.
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
对图上的点进行编号,然后维护每个黑点的能够向左和向右能延伸的最远的列,故用两个很并查集就好,一个父亲节点表示能向左延伸最远的列,一个是向右。
每个黑点和周围的8个点都算是联通(比赛时我按的是4联通一直WA,赛后一分钟就发现了这个问题)
然后对于每个加进来的点,先将该点与周围联通,然后Find一下,看该点是否能够向左能延伸到0列,向右能延伸到m-1列,如果满足,即为答案。
二分答案+bfs应该也是可以的。
/****************
*PID:
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}
const int maxn=500+10;
int mp[maxn][maxn],X[maxn*maxn],Y[maxn*maxn];
int n,m,par[maxn*maxn],par2[maxn*maxn];
char str[maxn];
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};
struct node{
int x,y;
}s[maxn];
void Init(){
for(int i=0;i<=n*m;i++){
par[i]=i;par2[i]=i;
}
}
int ID(int i,int j){
return i*m+j;
}
int Find(int x){
return x==par[x]?x:par[x]=Find(par[x]);
}
int Find2(int x){
return x==par2[x]?x:par2[x]=Find2(par2[x]);
}
void Union(int u,int v){
u=Find(u);v=Find(v);
if(u%m<v%m) par[v]=u;//维护最左列
else par[u]=v;
}
void Union2(int u,int v){
u=Find2(u);v=Find2(v);
if(u%m>v%m) par2[v]=u; //维护最右列
else par2[u]=v;
}
int main()
{
int i,j,T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
Init();
for(i=0;i<n;i++){
scanf("%s",str);
for(j=0;j<m;j++)
mp[i][j]=(str[j]-'0');
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(mp[i][j]==0) continue;
for(int k=0;k<8;k++){
int x=i+dx[k],y=j+dy[k];
if(x<0 || x>=n || y<0 || y>=m || mp[x][y]==0) continue;
Union(ID(i,j),ID(x,y));
Union2(ID(i,j),ID(x,y));
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++)
Find(ID(i,j));Find2(ID(i,j));
}
int q;
scanf("%d",&q);
for(i=1;i<=q;i++){
scanf("%d%d",&X[i],&Y[i]);
}
for(i=1;i<=q;i++){
mp[X[i]][Y[i]]=1;
for(int k=0;k<8;k++){
int x=X[i]+dx[k],y=Y[i]+dy[k];
if(x<0 || x>=n || y<0 || y>=m || mp[x][y]==0) continue;
Union(ID(X[i],Y[i]),ID(x,y));
Union2(ID(X[i],Y[i]),ID(x,y));
}
int t=Find(ID(X[i],Y[i])),d=Find2(ID(X[i],Y[i]));
if(t%m==0 && d%m==m-1)
break;
}
if(i<=q)
printf("%d\n",i);
else puts("-1");
}
return 0;
}