链接: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;
}

CF 1130C Connect的更多相关文章

  1. mina通信 demo

    1,要用到4个jar 2,服务端 package mina.server; import java.io.IOException; import java.net.InetSocketAddress; ...

  2. Mina入门教程(二)----Spring4 集成Mina

    在spring和mina集成的时候,要十分注意一个问题:版本. 这是一个非常严重的问题,mina官网的demo没错,网上很多网友总结的代码也是对的,但是很多人将mina集成到spring中的时候,总是 ...

  3. Apache Mina实战

    Mina介绍 Mina可以用于快速的开发基于网络通信的应用,特别是在开发手机端的游戏应用时,使用的较为普遍.本文简单介绍了一个用Mina搭建的一个简易讨论组,通过该应用可以对Mina的基本用法用途有个 ...

  4. XMPP学习——2、用户登录

    最近在学习XMPP的使用,打算完成一个完整较为完整地Demo示例,通过这个示例掌握xmpp的使用与开发.同时打算在这个示例中学习使用一下其他的开源类库,在此作为记录学习. 包括服务器端--Openfi ...

  5. mima开发实列

    最顶层父基类Clinet:用于记录公共内容 切供多个Clinet继承公用 import java.net.InetSocketAddress; import java.nio.charset.Char ...

  6. java-通讯stocket插件mina实例

    mina是对nio的具体实现.是目前比较高效和流行的nio(非阻塞式I/O)框架 mina主要包括: 其中服务端为:NioSocketAcceptor 客户端为:NioSocketConnector ...

  7. Apache Mina(一)

    原文链接:http://www.cnblogs.com/xuekyo/archive/2013/03/06/2945826.html Apache Mina是一个能够帮助用户开发高性能和高伸缩性网络应 ...

  8. MINA经典入门例子----Time Server

    原文地址 http://blog.sina.com.cn/s/blog_720bdf0501010b8r.html 貌似java的IO.NIO的入门例子都有相关的Time Server Demo.本例 ...

  9. Ui篇--layout_weight体验(实现按比例显示)

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

随机推荐

  1. Django 数据库配置

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'books', #你的数据库名称 'USER': ' ...

  2. word 2007 文档中添加文件

    1. "插入"->"对象",出现的下拉框中选择"对象" 2.弹出界面 3.切换至"由文件创建",点击"浏 ...

  3. 创建简单web项目

    Intellij Idea直接安装(可根据需要选择自己设置的安装目录),jdk使用1.6/1.7/1.8都可以,主要是配置好系统环境变量,tomcat7上tomcat的官网下载压缩包解压即可. 一.创 ...

  4. 面试官,不要再问我“Java 垃圾收集器”了(转载)

    如果Java虚拟机中标记清除算法.标记整理算法.复制算法.分代算法这些属于GC收集算法中的方法论,那么"GC收集器"则是这些方法论的具体实现. 在 面试过程中这个深度的问题涉及的比 ...

  5. hdu 3388 Coprime

    第一个容斥的题,感觉这东西好神啊.于是扒了一发题解2333 首先想对于[1,x]内有多少与n,m都互质的数,显然x是存在单调性的,所以可以二分一下. 那么互质的数的求法,就是x-存在n,m一个质因数的 ...

  6. Dubbo+Zookeeper 入门Demo

    1.Zookeeper安装及启动 可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html 从下载到启动都描述的很详细,按照文章一步一步走即可. 2.D ...

  7. vps 跑流量

  8. 吴裕雄--天生自然C++语言学习笔记:C++ 变量作用域

    作用域是程序的一个区域,一般来说有三个地方可以定义变量: 在函数或一个代码块内部声明的变量,称为局部变量. 在函数参数的定义中声明的变量,称为形式参数. 在所有函数外部声明的变量,称为全局变量. 局部 ...

  9. 201771010123汪慧和《面向对象程序设计Java》第十五周实验总结

    一.理论部分 1.JAR文件 (1)Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar文件后,GUI界面 程序就可以直接双击图标运行. (2).jar文件(Java归档)既 ...

  10. iOS如何禁用长按页面弹出菜单

    iOS如何禁止用户长按页面导致弹出菜单? 给元素设置样式: -webkit-touch-callout:none; 补充:同样适用于图片如果想禁止用户保存或者复制等