India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 920 Accepted Submission(s): 313
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.
![](https://i-blog.csdnimg.cn/blog_migrate/2ab43268d14885b0afe56f5d8ee89fa3.jpeg)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/110dfbae428eba2f8fbc07d244b72392.jpeg)
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
并查集,可以考虑从地图左边到右边是否出现一条路阻断联通,即 左边某点到右边某点是否有关系,就可以判断上下联通。
用f[0]和f[n * m + 1]分别表示左右端点, 只要判断他们是否连通即可, 连通则两国隔绝。
有一点一直不懂,我这代码用g++交一直爆内存,用c++叫就可以过了噶![抓狂](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const int inf = 1000000005;
const long long mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中
int f[250005], a[505][505];
char s[505];
int vis[505][505], n, m;
int find(int x)
{
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
void add(int x, int y)
{
int fa = find(vis[x][y]);
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
if (a[i + x][j + y] == 1)
{
int fb = find(vis[i + x][j + y]);
f[fb] = fa;
}
}
}
if (y == 1)
f[fa] = f[find(0)];
if (y == m)
f[fa] = f[find(n * m + 1)];
a[x][y] = 1;
}
int main()
{
int t, i, j, q;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
memset(a, 0, sizeof(a));
int x = 1;
f[0] = 0, f[n * m + 1] = n * m + 1;
for (i = 1; i <= n; ++i)
{
scanf("%s", s);
for (j = 1; j <= m; ++j)
{
vis[i][j] = x;
f[x] = x++;
a[i][j] = s[j - 1] - '0';
if (s[j - 1] - '0' == 1)
{
add(i, j);
}
}
}
int ans = -1;
if (find(0) == find(n * m + 1))
ans = 0;
scanf("%d", &q);
for (i = 1; i <= q; ++i)
{
int xx, yy;
scanf("%d%d", &xx, &yy);
add(xx + 1, yy + 1);
if (find(0) == find(n * m + 1) && ans == -1)
ans = i;
}
printf("%d\n", ans);
}
}