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 .

就是简单的搜索,用dfs求出连通块,然后就遍历起点的所有连通块到终点的连通块,求出最短距离。

#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = ;
char s[][];
int n, sx, sy, gx, gy,ans=inf;
int dx[] = { ,,-, };
int dy[] = { ,,,- };
bool vis[][];
struct node
{
int x, y;
node(int x = , int y = ) :x(x), y(y) {}
};
int cnt = ;
void dfs(int x, int y, node *d)
{
for (int i = ; i < ; i++)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (vis[tx][ty]) continue;
if (s[tx][ty] == '') continue;
if (tx< || ty< || tx>n || ty>n) continue; vis[tx][ty] = true;
d[cnt++] = node(tx, ty);
dfs(tx, ty, d);
}
}
int min_(int a,int b)
{
return a < b ? a : b;
} int main()
{
cin >> n >> sx >> sy >> gx >> gy;
for (int i = ; i <= n; i++) cin >> s[i] + ;
node a[maxn], b[maxn];
a[] = node(sx, sy); b[] = node(gx, gy);
vis[sx][sy] = true;cnt = ;
dfs(sx, sy, a);
if(vis[gx][gy])
{
printf("0\n");
return ;
}
int tot = cnt;cnt = ;
vis[gx][gy] = ;
dfs(gx, gy, b);
//printf("%d %d\n", cnt, tot);
for(int i=;i<tot;i++)
{
for(int j=;j<cnt;j++)
{
//printf("%d %d %d %d\n", a[i].x, a[i].y, b[j].x, b[j].y);
int exa = (a[i].x - b[j].x)*(a[i].x - b[j].x) + (a[i].y - b[j].y)*(a[i].y - b[j].y);
//printf("%d\n", exa);
ans = min_(ans, exa);
}
}
printf("%d\n", ans);
return ;
}

Codeforces Round #542 C. Connect 搜索的更多相关文章

  1. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  2. Codeforces Round #542 题解

    Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...

  3. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  4. Codeforces Round #542(Div. 2) C.Connect

    链接:https://codeforces.com/contest/1130/problem/C 题意: 给一个n*n的图,0表示地面,1表示水,给出起点和终点, 现要从起点到达终点,有一次在两个坐标 ...

  5. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2

    A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...

  6. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

    A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...

  7. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)

    http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...

  8. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  9. Codeforces Round #542(Div. 2) B.Two Cakes

    链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...

随机推荐

  1. oracle expdp自动备份脚本

    windows: @echo off echo ================================================ echo Windows环境下Oracle数据库的自动 ...

  2. asp.net-常用服务器控件-20180329

    常用服务器控件 1.文本类型控件 Label控件 TextBox控件 2.按钮类型控件 Button控件 ImageButton控件 3.选择类型控件 CheckBox控件 RadioButton控件 ...

  3. python文件夹copy器(多进程版)

    本节的练习的要求如下: 输入要拷贝文件夹的文件名称 读取该文件夹下的所有文件 启动5个进程来拷贝文件夹,将拷贝成功的文件名称放入队列中 主进程中显示文件拷贝的进度 代码如下: import multi ...

  4. linux 下 ifcfg-ethx配置和解析

    网络接口配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM ...

  5. xml方式封装数据方法

    1.xml方式封装数据方法 2.demo <?php xml方式封装数据方法 /** * [xmlEncode description] * @param [type] $code [descr ...

  6. CSS用法总结(持续更新)

    一.html,body{height:100%} 解决了容器高度不足(容器高度由子元素高度决定,而%按照父元素的百分比),无法用%布局页面的问题 把html和body的高度设置为浏览器高度,此时会出现 ...

  7. Spring 切入点配置

    有关各种拦截的切入点配置举例 (1)只对返回值为String的方法进行拦截 @Pointcut("execution (java.lang.String com.zzdr.spring.se ...

  8. 【代码笔记】Web-JavaScript-JavaScript正则表达式

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  9. 如何获取view的大小

    很多初学者都会犯一个错误 ,就是在onCreate或者onStart里面去获取view的大小,然而这样获取到的宽高通常都是0,为什么呢?因为view的测量过程和activity的生命周期不是同步的,因 ...

  10. ScrollView嵌套ListView、GridView,进入页面显示的位置并不是在最顶部,而是在中间部分问题

    在Android项目的开发中,经常会遇到一些布局,可能需要在ScrollView中嵌套ListView或.GridView来实现, 是在使用的过程总又遇到了一个新的问题,就是如果在ScrollView ...