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.
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
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.
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
在hdu上提交的 一直wa 以为是过程有问题 最后发现数组开小了 一改就对了 自我感觉这是一道比较水的题 主要和二分挂钩 下面广搜来一波
#include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<math.h> using namespace std; #define INF 0x3f3f3f3f #define LL long long #define N 506 char str[N][N]; int w[N][N],aa[N*N],b[N*N],d,n,m; int a[4][2]= {{-1,0},{1,0},{0,1},{0,-1}}; struct node { int x,y; }; int qq(int x,int y) { memset(w,0,sizeof(w)); queue<node> Q; node q,p; q.x=x; q.y=y; Q.push(q); w[x][y]=1; while(Q.size()) { p=Q.front(); Q.pop(); if(p.x==n-1)///表示可以到底 return 1; for(int i=0; i<4; i++) { q.x=p.x+a[i][0]; q.y=p.y+a[i][1]; int e=q.x,f=q.y; if(e>=0&&e<n&&f>=0&&f<m&&!w[e][f]&&str[e][f]==‘0‘) { w[e][f]=1; Q.push(q); } } } return 0;///无到底的点 } void qqq(int x) { for(int i=1; i<=x; i++) str[aa[i]][b[i]]=‘0‘; } int q(int x) { for(int i=1; i<=x; i++) str[aa[i]][b[i]]=‘1‘; for(int j=0; j<m; j++) if(str[0][j]==‘0‘) { if(qq(0,j))///如果已经存在到底的点 则返回0 结束 return 0; } return 1; } int main() { int T,t; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0; i<n; i++) scanf("%s",str[i]); scanf("%d",&t); for(int i=1; i<=t; i++) scanf("%d%d",&aa[i],&b[i]); int l=1,r=t,mid,ans=-1; while(l<=r) { mid=(l+r)/2; if(q(mid))///如果已经不满足 ans提取出来 继续减少范围 { ans=mid; r=mid-1; } else l=mid+1; qqq(mid);///因为你改变了一部分数 就重新改变过来 } printf("%d\n",ans); } return 0; }
深搜也可以的
#include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<math.h> using namespace std; #define INF 0x3f3f3f3f #define LL long long #define N 506 char str[N][N]; int w[N][N],aa[N],b[N],d,n,m; int a[4][2]= {{-1,0},{1,0},{0,1},{0,-1}}; void qq(int x,int y) { if(d==1) return ; for(int i=0; i<4; i++) { int e=x+a[i][0]; int f=y+a[i][1]; if(e==n-1&&str[e][f]==‘0‘) { d=1; return ; } if(e>=0&&e<n&&f>=0&&f<m&&!w[e][f]&&str[e][f]==‘0‘) { w[e][f]=1; qq(e,f); } } return ; } void qqq(int x) { for(int i=1; i<=x; i++) str[aa[i]][b[i]]=‘0‘; } int q(int x) { for(int i=1; i<=x; i++) str[aa[i]][b[i]]=‘1‘; for(int j=0; j<m; j++) if(str[0][j]==‘0‘) { d=0; memset(w,0,sizeof(w)); qq(0,j); if(d==1) return 0; } return 1; } int main() { int T,t; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0; i<n; i++) scanf("%s",str[i]); scanf("%d",&t); for(int i=1; i<=t; i++) scanf("%d%d",&aa[i],&b[i]); int l=1,r=t,mid,ans=-1; while(l<=r) { mid=(l+r)/2; if(q(mid)) { ans=mid; r=mid-1; } else l=mid+1; qqq(mid); } printf("%d\n",ans); } return 0; }