Alice lives on a flat planet that can be modeled as a square grid of size n×n, with rows and columns enumerated from 1 to n. We represent the cell at the intersection of row r and column c with ordered pair (r,c). Each cell in the grid is either land or water.
An example planet with n=5. It also appears in the first sample test.
Alice resides in land cell (r1,c1). She wishes to travel to land cell (r2,c2). At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).
Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice’s trip may be impossible.
To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells (rs,cs) and (rt,ct) is (rs−rt)2+(cs−ct)2.
For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1) to (r2,c2). If no tunnel needs to be created, the cost is 0.
Input
The first line contains one integer n (1≤n≤50) — the width of the square grid.
The second line contains two space-separated integers r1 and c1 (1≤r1,c1≤n) — denoting the cell where Alice resides.
The third line contains two space-separated integers r2 and c2 (1≤r2,c2≤n) — denoting the cell to which Alice wishes to travel.
Each of the following n lines contains a string of n characters. The j-th character of the i-th such line (1≤i,j≤n) is 0 if (i,j) is land or 1 if (i,j) is water.
It is guaranteed that (r1,c1) and (r2,c2) are land.
Output
Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1) to (r2,c2).
Examples
Input
5
1 1
5 5
00001
11111
00111
00110
00110
Output
10
Input
3
1 3
3 1
010
101
010
Output
8
Note
In the first sample, a tunnel between cells (1,4) and (4,5) should be created. The cost of doing so is (1−4)2+(4−5)2=10, which is optimal. This way, Alice could walk from (1,1) to (1,4), use the tunnel from (1,4) to (4,5), and lastly walk from (4,5) to (5,5).
In the second sample, clearly a tunnel between cells (1,3) and (3,1) needs to be created. The cost of doing so is (1−3)2+(3−1)2=8.
思路:求起点和终点所能到达的范围,暴力求最小值;
下面展示一些 内联代码片
。
#include<bits/stdc++.h>
using namespace std;
int vis[1001][1001],a[1001][1001];
char a1[1001][1001];
int dir[4][2]= {1,0,-1,0,0,1,0,-1};//方向
int n;
struct stu
{
int x;
int y;
} z[10010],q[10010]; //记录起点和终点所能到的点
void dfs(int x,int y)
{
for(int i=0; i<=3; i++)
{
if((x+dir[i][0]>=1)&&(x+dir[i][0]<=n)&&(y+dir[i][1]>=1)&&(y+dir[i][1]<=n)&&(a[x+dir[i][0]][y+dir[i][1]]==0)&&(vis[x+dir[i][0]][y+dir[i][1]]==0))
{
vis[x+dir[i][0]][y+dir[i][1]]=1;
dfs(x+dir[i][0],y+dir[i][1]);
}
}
}
int main()
{
int x1,x2,y1,y2;
int i,j;
cin>>n;
cin>>x1>>y1;
cin>>x2>>y2;
for(i=1; i<=n; i++)
{
cin>>a1[i];
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(a1[i][j-1]=='0')a[i][j]=0;
else a[i][j]=1;
}
}
int cnt1=0;//记录起点到达范围的数量。
vis[x1][y1]=1;
dfs(x1,y1);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
if(vis[i][j]==1)
{
cnt1++;
q[cnt1].x=i;
q[cnt1].y=j;
}
}
memset(vis,0,sizeof(vis));
int cnt2=0;//记录起点到达范围的数量。
vis[x2][y2]=1;
dfs(x2,y2);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
if(vis[i][j]==1)
{
cnt2++;
z[cnt2].x=i;
z[cnt2].y=j;
}
}
int min=1000000000;
for(i=1; i<=cnt1; i++)
{
for(j=1; j<=cnt2; j++)
{
if((z[j].x-q[i].x)*(z[j].x-q[i].x)+(z[j].y-q[i].y)*(z[j].y-q[i].y)<min)
min=(z[j].x-q[i].x)*(z[j].x-q[i].x)+(z[j].y-q[i].y)*(z[j].y-q[i].y);
}
}
cout<<min<<endl;
}