C. Connect
http://codeforces.com/contest/1130/problem/C
50*50 的图 0代表陆地 1代表水
给你起点终点,可以上下左右走陆地,问你起点这块和终点那块各选一个可以走的方位算距离,输出最小的
直接暴力bfs起点和终点,然后把能走的互相配
#include <bits/stdc++.h>
using namespace std;
char a[55][55];
int walk[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y;
};
queue <node> q,q1;
int vis[55][55],vis1[55][55],n;
void bfs(int s,int t,int judge)
{
node qi;
qi.x=s,qi.y=t;
q.push(qi);
if(judge) vis[s][t]=1;
else vis1[s][t]=1;
while(!q.empty())
{
node p=q.front();
q.pop();
for(int i=0; i<4; i++)
{
int xx=p.x+walk[i][0],yy=p.y+walk[i][1];
if(xx<=0||xx>n||yy<=0||yy>n||a[xx][yy]=='1') continue;
if(judge&&vis[xx][yy]) continue;
else if(!judge&&vis1[xx][yy]) continue;
if(judge) vis[xx][yy]=1;
else vis1[xx][yy]=1;
node o;
o.x=xx,o.y=yy;
q.push(o);
}
}
}
int main()
{
cin>>n;
int sx,sy,tx,ty;
cin>>sx>>sy>>tx>>ty;
for(int i=1; i<=n; i++)
{
scanf("%s",a[i]+1);
}
bfs(sx,sy,1);
bfs(tx,ty,0);
//
// for(int i=1; i<=n; i++)
// {
// for(int j=1; j<=n; j++)
// {
// if(vis[i][j]) cout<<i<<" "<<j<<endl;
// }
// }
//
// cout<<endl;
//
// for(int i=1; i<=n; i++)
// {
// for(int j=1; j<=n; j++)
// {
// if(vis1[i][j]) cout<<i<<" "<<j<<endl;
// }
// }
int minn=1e8;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
for(int i1=1; i1<=n; i1++)
{
for(int j1=1; j1<=n; j1++)
{
if(vis[i][j]&&vis1[i1][j1])
{
minn=min(minn,(i-i1)*(i-i1)+(j-j1)*(j-j1));
}
}
}
}
}
printf("%d\n",minn);
return 0;
}