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
一题不错的并查集,之前一直在想怎么确定是否连接,想过保存最左点和最右点,想过保存最长的距离;
最后还是模拟两个点,s,t ,当有点的y是1的时候和s连接,y是n的时候和t连接;
用并查集维护,最后枚举时间,检查下s和t是否连接就行;
1A,开心;
有人用二分来算,然而我觉的二分的时候要把点重新添加进去,可能并不会快;
最后没用二分60ms,用了300+ms,当然也有可能用二分不用添加点什么的可能更快;
然而我还没想到怎么做……
代码:(未二分)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int pre[300005];
int m,n;
int q;
int flag;
char data[505][505];
int vis [505][505];
struct node
{
int x;
int y;
}time[250005];
int find (int x)
{
int r =x;
if(pre[r]!=r)
pre[r]=find(pre[r]);
return pre[r];
}
void join (int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
return;
fy = find(y);
if(fx!=fy)
pre[fx]=pre[fy];
}
void init(int n)
{
int i;
pre[0]=0;
pre[300000]=300000;
for(i=1;i<=n;++i)
pre[i] = i;
memset(vis,0,sizeof(vis));
}
int solve(int x,int y)
{
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
if(vis[x+i][y+j]==1)
join( n*(x-1)+y,(x+i-1)*n+ y+j);
if(y==1)
join(n*(x-1)+y,0);
if(y==n)
join(n*(x-1)+y,300000);
vis[x][y]=1;
if(find(0)==find(300000))
return 1;
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
int i,j;
init( (m+1)*(n+1));
for(i=1;i<=m;i++)
{
scanf("%s",data[i]+1);
for(j=1;j<=n;j++)
{
if(data[i][j]=='1')
solve(i,j);
}
}
scanf("%d",&q);
for(i=1;i<=q;i++)
scanf("%d%d",&time[i].x,&time[i].y);
if(find(0)==find(300000))
{
printf("0\n");
continue;
}
for(i=1;i<=q;i++)
{
if(solve(time[i].x+1,time[i].y+1))
break;
}
if(i==1+1)
printf("-1\n");
else
printf("%d\n",i);
}
return 0;
}