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.
题目大意,每年都会有一座山升起来问几年后中印的交流会断掉
我的ma,居然没有意识到年是单调的,既然意识到了,辣么就可以二分了
对于每一个mid年份 先绘制一下图tgap 然后从第一排的每一个位置开始搜索,如果能搜到n-1的位置,那么就可以交流,继续向上二分,否则向下二分。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,year;
char gap[600][600];
char tgap[600][600];
int px[251000],py[251000];
bool visit[550][550];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
struct node
{
int x,y;
node(){}
node(int a,int b)
{
x=a,y=b;
}
};
bool bfs(int x,int y)
{
memset(visit,0,sizeof(visit));
visit[x][y]=1;
queue<node> q;
q.push(node(x,y));
while(!q.empty())
{
node in,ne;
in=q.front();
q.pop();
if(in.x>=n-1)
{
return 1;
}
for(int i=0;i<4;i++)
{
ne.x=in.x+dir[i][0];
ne.y=in.y+dir[i][1];
if(visit[ne.x][ne.y]==0&&tgap[ne.x][ne.y]=='0'&&ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m)
{
visit[ne.x][ne.y]=1;
q.push(ne);
}
}
}
return false;
}
bool judge(int Y)
{
for(int i=0;i<n;i++) strcpy(tgap[i],gap[i]);
for(int i=1;i<=Y;i++)
{
tgap[px[i]][py[i]]='1';
}
for(int i=0;i<m;i++)
{
if(tgap[0][i]=='0')
{
bool flag=bfs(0,i);
if(flag) return true;
}
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",gap[i]);
scanf("%d",&year);
for(int i=1;i<=year;i++)
{
int x,y;
scanf("%d%d",&x,&y);
px[i]=x,py[i]=y;
}
int l=0,r=year,mid=0,ans=0;
while(r>=l)
{
mid=(l+r)>>1;
if(judge(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans+1);
}
return 0;
}