India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 733 Accepted Submission(s): 252
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 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:给你一个你n*m的矩阵,其中0表示可以走,1表示不能走,再有若干个操作,即将一些为0的点改成1,问最少多少个操作之后,不能从第一行走到最后一行。 分析: 1.dfs是必要的,搜索(bfs也是可行的); 2,如何判断到底要多少个点才能不走通,这用二分的思想是很好做的,对用到的点数进行二分,当然想到是有些困难的,至少当时我没想到。 代码:#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=501; char s[maxn][maxn]; int num1[maxn][maxn],num2[maxn][maxn],vis[maxn][maxn]; struct node { int x,y; }A[maxn*maxn]; int n,m,flag; void dfs(int x,int y) { if(x<0||y<0||x>n-1||y>m-1) return ; if(num2[x][y]||vis[x][y]) return ; vis[x][y]=1; if(x==n-1) { flag=1; return ; } dfs(x-1,y); dfs(x+1,y); dfs(x,y-1); dfs(x,y+1); } int main() { int T; while(scanf("%d",&T)!=EOF) while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",s[i]); memset(num1,0,sizeof(num1)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(s[i][j]=='1') num1[i][j]=1; int p; scanf("%d",&p); int a,b; for(int i=0;i<p;i++) { scanf("%d%d",&a,&b); A[i].x=a; A[i].y=b; } int l=0,r=p; while(l<r) { flag=0; int m1=(l+r)>>1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(num1[i][j]==1) num2[i][j]=1; else num2[i][j]=0; } for(int i=0;i<=m1;i++) { int x=A[i].x,y=A[i].y; num2[x][y]=1; } // for(int i=0;i<n;i++) // { // for(int j=0;j<m;j++) // printf("%d",num2[i][j]); // printf("\n"); // } memset(vis,0,sizeof(vis)); for(int i=0;i<m;i++) if(!vis[0][i]) dfs(0,i); if(!flag) r=m1; else l=m1+1; } if(r==p) printf("-1\n"); else printf("%d\n",l+1); } return 0; }