http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=682&pid=1003
思路:
二分最先 A 不能到达B 的值 ,判断A 是否能到达B 广搜 标记 就可了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
char a[maxn][maxn];
char b[maxn][maxn];
int vis[maxn][maxn];
int dri[4][2] = {{0,1},{0,-1},{-1,0},{1,0} };
struct node
{
int x,y ;
} c[maxn*maxn];
int n,m;
bool bfs()
{ memset(vis,0,sizeof(vis));
queue<node>q;
node s;
for(int j= 0; j<m; j++)
{
if(b[0][j]=='0')
{
s.x= 0;
s.y =j;
q.push(s);
}
}
while(!q.empty())
{
node now = q.front();
q.pop();
if(now.x==n-1)
return true;
for(int i = 0; i<4; i++)
{
node next ;
next.x = now.x+dri[i][0];
next.y = now.y +dri[i][1];
if(vis[next.x][next.y]==0&&next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&b[next.x][next.y]=='0')
{
q.push(next);
vis[next.x][next.y] =1;
}
}
}
return false ;
}
bool slove(int x)
{ memset(b,0,sizeof(b));
for(int i = 0; i<n; i++)
for(int j =0; j<m; j++)
{
b[i][j] = a[i][j];
}
for(int i = 1; i<=x; i++)
{
b[c[i].x][c[i].y] = '1';
}
if(bfs())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",a[i]);
int q;
cin>>q;
for(int i = 1; i<=q; i++)
scanf("%d%d",&c[i].x,&c[i].y);
int l = 1;
int r = q;
while(l<=r)
{
int mid = (l+r)>>1;
if(slove(mid))
{
l = mid+1;
}
else r = mid-1;
}
if(l==q){puts("-1");continue;}
printf("%d\n",l);
}
return 0;
}