CF1130C.Connect

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Alice lives on a flat planet that can be modeled as a square grid of size n×nn \times 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)(r_1, c_1). She wishes to travel to land cell (r2,c2)(r_2, c_2). 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)(r_s, c_s) and (rt,ct)(r_t, c_t) is (rs−rt)2+(cs−ct)2(r_s-r_t)^2 + (c_s-c_t)^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)(r_1, c_1) to (r2,c2)(r_2, c_2). If no tunnel needs to be created, the cost is 00.

Alice 生活在一个可建模为 n×nn \times n 方形网格的平坦星球上,行与列均从 11 编号至 nn。我们用有序对 (r,c)(r, c) 表示第 rr 行与第 cc 列相交的格子。网格中每个格子要么是陆地,要么是水域。

一个 n=5n = 5 的星球示例。该图也出现在第一个样例测试中。

Alice 居住在陆地格子 (r1,c1)(r_1, c_1)。她希望前往另一陆地格子 (r2,c2)(r_2, c_2)。在任意时刻,她可向当前所在位置的四个相邻格子之一移动(即向上、向下、向左或向右)。

不幸的是,Alice 不会游泳,且除步行外没有其他可行的交通方式(即她只能在陆地上行走)。因此,Alice 的行程可能无法实现。

为帮助 Alice,你计划至多修建一条连接两个陆地格子的隧道。隧道将允许 Alice 在其两个端点之间自由通行。实际上,修建隧道需要大量工作:在格子 (rs,cs)(r_s, c_s) 与 (rt,ct)(r_t, c_t) 之间修建隧道的成本为 (rs−rt)2+(cs−ct)2(r_s-r_t)^2 + (c_s-c_t)^2。

目前,你的任务是求出修建至多一条隧道的最小可能成本,使得 Alice 能够从 (r1,c1)(r_1, c_1) 到达 (r2,c2)(r_2, c_2)。若无需修建隧道,成本为 00。

输入格式

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

The second line contains two space-separated integers r1r_1 and c1c_1 (1≤r1,c1≤n1 \leq r_1, c_1 \leq n) — denoting the cell where Alice resides.

The third line contains two space-separated integers r2r_2 and c2c_2 (1≤r2,c2≤n1 \leq r_2, c_2 \leq 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 \leq i, j \leq 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)(r_1, c_1) and (r2,c2)(r_2, c_2) are land.

第一行包含一个整数 nn(1≤n≤501 \leq n \leq 50)——表示正方形网格的宽度。

第二行包含两个以空格分隔的整数 r1r_1 和 c1c_1(1≤r1,c1≤n1 \leq r_1, c_1 \leq n)——表示爱丽丝所在的位置。

第三行包含两个以空格分隔的整数 r2r_2 和 c2c_2(1≤r2,c2≤n1 \leq r_2, c_2 \leq n)——表示爱丽丝希望前往的位置。

接下来的 nn 行中,每行包含一个长度为 nn 的字符串。其中第 ii 行(1≤i≤n1 \leq i \leq n)的第 jj 个字符(1≤j≤n1 \leq j \leq n)为 0 表示位置 (i,j)(i, j) 是陆地,为 1 表示位置 (i,j)(i, j) 是水域。

保证 (r1,c1)(r_1, c_1) 和 (r2,c2)(r_2, c_2) 均为陆地。

输出格式

Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r_1, c_1) to (r2,c2)(r_2, c_2).

输出一个整数,表示在最多修建一条隧道的前提下,爱丽丝从 (r1,c1)(r_1, c_1) 到达 (r2,c2)(r_2, c_2) 所需的最小可能代价。

输入输出样例

  • 输入#1

    5
    1 1
    5 5
    00001
    11111
    00111
    00110
    00110

    输出#1

    10
  • 输入#2

    3
    1 3
    3 1
    010
    101
    010

    输出#2

    8

说明/提示

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.

在第一个样例中,应在格子 (1,4)(1, 4) 和 (4,5)(4, 5) 之间修建一条隧道。修建该隧道的代价为 (1−4)2+(4−5)2=10(1-4)^2 + (4-5)^2 = 10,这是最优方案。这样,爱丽丝可以从 (1,1)(1, 1) 走到 (1,4)(1, 4),再通过隧道从 (1,4)(1, 4) 到达 (4,5)(4, 5),最后从 (4,5)(4, 5) 走到 (5,5)(5, 5)。

在第二个样例中,显然需要在格子 (1,3)(1, 3) 和 (3,1)(3, 1) 之间修建一条隧道。修建该隧道的代价为 (1−3)2+(3−1)2=8(1-3)^2 + (3-1)^2 = 8。

输入解题思路,AI测评打分。不知道怎么写?

首页