链接:https://codeforces.com/contest/1130/problem/C

C. Connect

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Alice lives on a flat planet that can be modeled as a square grid of size n×nn×n , with rows and columns enumerated from 11 to nn . We represent the cell at the intersection of row rr and column cc with ordered pair (r,c)(r,c) . Each cell in the grid is either land or water.

An example planet with n=5n=5 . It also appears in the first sample test.

Alice resides in land cell (r1,c1)(r1,c1) . She wishes to travel to land cell (r2,c2)(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)(rs,cs) and (rt,ct)(rt,ct) is (rs−rt)2+(cs−ct)2(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)(r1,c1) to (r2,c2)(r2,c2) . If no tunnel needs to be created, the cost is 00 .

Input

The first line contains one integer nn (1≤n≤501≤n≤50 ) — the width of the square grid.

The second line contains two space-separated integers r1r1 and c1c1 (1≤r1,c1≤n1≤r1,c1≤n ) — denoting the cell where Alice resides.

The third line contains two space-separated integers r2r2 and c2c2 (1≤r2,c2≤n1≤r2,c2≤n ) — denoting the cell to which Alice wishes to travel.

Each of the following nn lines contains a string of nn characters. The jj -th character of the ii -th such line (1≤i,j≤n1≤i,j≤n ) is 0 if (i,j)(i,j) is land or 1 if (i,j)(i,j) is water.

It is guaranteed that (r1,c1)(r1,c1) and (r2,c2)(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)(r1,c1) to (r2,c2)(r2,c2) .

Examples

Input

Copy

5
1 1
5 5
00001
11111
00111
00110
00110

Output

Copy

10

Input

Copy

3
1 3
3 1
010
101
010

Output

Copy

8

Note

In the first sample, a tunnel between cells (1,4)(1,4) and (4,5)(4,5) should be created. The cost of doing so is (1−4)2+(4−5)2=10(1−4)2+(4−5)2=10 , which is optimal. This way, Alice could walk from (1,1)(1,1) to (1,4)(1,4) , use the tunnel from (1,4)(1,4) to (4,5)(4,5) , and lastly walk from (4,5)(4,5) to (5,5)(5,5) .

In the second sample, clearly a tunnel between cells (1,3)(1,3) and (3,1)(3,1) needs to be created. The cost of doing so is (1−3)2+(3−1)2=8(1−3)2+(3−1)2=8 .

题意:给出n*n的矩阵、起点以及终点,每个格子有0或者1,0代表陆地,1代表水。现在有个人想从起点走到终点,但他不能沾水。现在你可以修最多一条管道,连接两块陆地,费用为相应两点间距离的平方。问最终最小的费用为多少。

思路:找到S可以直接到达的点,找到T直接可以到达的点,然后找这两个点集中两两距离平方最小的就可以了。

代码中有注解

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
int x[2],y[2];//起点和终点
char z[55][55];
int v[2][55][55];//记录能通过的点
int ans=INT_MAX;
void dfs(int a,int b,int c)
{
if(v[c][a][b])
return ;
v[c][a][b]=1;
if(z[a+1][b]=='0')//如果(a+1,b)可以走,继续搜索。下面同理
dfs(a+1,b,c);
if(z[a][b+1]=='0')
dfs(a,b+1,c);
if(z[a-1][b]=='0')
dfs(a-1,b,c);
if(z[a][b-1]=='0')
dfs(a,b-1,c);
}
int main()
{
cin>>n;
for(int i=0;i<2;i++)
cin>>x[i]>>y[i];
for(int i=1;i<=n;i++)
scanf("%s",z[i]+1);
for(int i=0;i<2;i++)
dfs(x[i],y[i],i);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(v[0][i][j])//起点可以到达的位置
{
for(int k=1;k<=n;k++)
for(int l=1;l<=n;l++)
{
if(v[1][k][l])//终点可以到达的位置
ans=min(ans,(i-k)*(i-k)+(j-l)*(j-l));
}
}
}
cout<<ans<<endl;
}

最新文章

  1. 基于Mono.Cecil的静态注入
  2. 【python】判断字符串日期是否有效
  3. 立即执行函数与window.onload作用类似
  4. gomoblie flappy 源码分析:图片素材和大小的处理
  5. Hadoop Oozie 学习笔记
  6. DOS命令教学之详解批处理
  7. Content-Language:en-US
  8. MySQL 二进制日志过滤
  9. C#二维码生成解析
  10. pthread的lowlevellock
  11. Problem B: 开个餐馆算算账
  12. poj 2774 最长公共子串 后缀数组
  13. 窗口函数解决数据岛问题(mysql暂无窗口函数,可以通过用户变量解决窗口函数问题)
  14. 逆向暴力求解 538.D Weird Chess
  15. HashMap在JDK1.7中可能出现的并发问题
  16. RunC容器逃逸漏洞席卷业界,网易云如何做到实力修复?
  17. c# UTF-16转UTF-8 互转
  18. Linux下阅读MHT文件
  19. [转载]C# 常用日期时间函数(老用不熟)
  20. cookie的常用操作

热门文章

  1. SpringBoot安全认证Security
  2. POJ 3096:Surprising Strings
  3. weex 随笔
  4. 十七、CI框架之数据库操作insert用法
  5. python单链表的基本操作思路
  6. axios基础介绍
  7. POJ 1887:Testing the CATCHER 求递减序列的最大值
  8. Delphi生成即调用带窗体的Dll
  9. UML-领域模型的精化
  10. ftp限制