India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 333
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 Qlines 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 Qlines 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
Source
这个题直接dfs搜黑色的从左到右的路会超时,按照官方题解:先离线处理并查集(就是把所有的Q年里出现的所有山峰都直接按照1来处理),然后再照年份从后往前的顺序把新出现的平原加入并查集,记住要给起点和终点设两个新节点,不然只能循环查找(x=1)的节点来处理,会超时。。。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int Max=511; int n,m; int cnt; char ch[Max]; bool used[Max][Max]; int mat[Max][Max]; int a[Max][Max]; struct node { int x,y; } que[Max*Max],qq[Max*Max]; int f[Max*Max]; const int inf=Max*Max-1; int find(int x) { if(f[x]==x) return x; return f[x]=find(f[x]); } void Merge(int i,int j) { int t1=find(i),t2=find(j); if(t1!=t2) { f[t2]=t1; } } int dx[]= {1,-1,0,0}; int dy[]= {0,0,1,-1}; void solve() { for(int i=1; i<=cnt; i++) { if(used[que[i].x][que[i].y]) continue; for(int k=0; k<4; k++) { int x=que[i].x+dx[k],y=que[i].y+dy[k]; if(x<1||y<1||x>n||y>m) continue; if(a[x][y]==1) continue; int j=mat[x][y]; if(used[x][y]) continue; Merge(i,j); } } } int main() { int T; for(scanf("%d",&T); T; T--) { scanf("%d%d",&n,&m); cnt=0; for(int i=1; i<=n; i++) { scanf("%s",ch); for(int j=0; j<strlen(ch); j++) { if(ch[j]=='0') { a[i][j+1]=0; que[++cnt].x=i; que[cnt].y=j+1; mat[i][j+1]=cnt; } else a[i][j+1]=1; } } int Q,x,y; int flag=0; memset(used,false,sizeof(used)); scanf("%d",&Q); for(int i=1; i<=Q; i++) { scanf("%d%d",&x,&y); x+=1; y+=1; used[x][y]=true; qq[i].x=x; qq[i].y=y; } for(int i=0; i<Max*Max; i++) f[i]=i; for(int i=1; i<=m; i++) { if(!used[1][i]) if(a[1][i]==0) Merge(inf-1,mat[1][i]); if(!used[n][i]) if(a[n][i]==0) Merge(inf,mat[n][i]); } solve(); // cout<<find(inf-1)<<" "<<find(inf)<<endl; int sum=Q; while(find(inf-1)!=find(inf)) { int x=qq[Q].x,y=qq[Q].y; used[x][y]=false; Q--; if(Q==-1) break; for(int k=0; k<4; k++) { int nx=x+dx[k],ny=y+dy[k]; if(nx<1||ny<1||nx>n||ny>m) continue; if(used[nx][ny]) continue; if(a[nx][ny]==0) Merge(mat[nx][ny],mat[x][y]); } if(x==1) Merge(mat[x][y],inf-1); if(x==n) Merge(mat[x][y],inf); } printf("%d ",Q==sum?-1:Q+1); } return 0; }