codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1130C.md (2932B)


      1 Source: https://codeforces.com/problemset/problem/1130/C
      2 
      3 C. Connect
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Alice lives on a flat planet that can be modeled as a square grid of size $n \times 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.
     22 
     23  ![](https://espresso.codeforces.com/e23260ca84ea65c1afac5deea5995132ef8b59e2.png) An example planet with $n = 5$. It also appears in the first sample test. 
     24 
     25 Alice resides in land cell $(r_1, c_1)$. She wishes to travel to land cell $(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).
     26 
     27 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.
     28 
     29 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 $(r_s, c_s)$ and $(r_t, c_t)$ is $(r_s-r_t)^2 + (c_s-c_t)^2$.
     30 
     31 For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from $(r_1, c_1)$ to $(r_2, c_2)$. If no tunnel needs to be created, the cost is $0$.
     32 
     33 Input
     34 
     35 The first line contains one integer $n$ ($1 \leq n \leq 50$) — the width of the square grid.
     36 
     37 The second line contains two space-separated integers $r_1$ and $c_1$ ($1 \leq r_1, c_1 \leq n$) — denoting the cell where Alice resides.
     38 
     39 The third line contains two space-separated integers $r_2$ and $c_2$ ($1 \leq r_2, c_2 \leq n$) — denoting the cell to which Alice wishes to travel.
     40 
     41 Each of the following $n$ lines contains a string of $n$ characters. The $j$-th character of the $i$-th such line ($1 \leq i, j \leq n$) is 0 if $(i, j)$ is land or 1 if $(i, j)$ is water.
     42 
     43 It is guaranteed that $(r_1, c_1)$ and $(r_2, c_2)$ are land.
     44 
     45 Output
     46 
     47 Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from $(r_1, c_1)$ to $(r_2, c_2)$.
     48 
     49 Examples
     50 
     51 Input
     52 
     53 ```
     54 5
     55 1 1
     56 5 5
     57 00001
     58 11111
     59 00111
     60 00110
     61 00110
     62 ```
     63 
     64 Output
     65 
     66 ```
     67 10
     68 ```
     69 
     70 Input
     71 
     72 ```
     73 3
     74 1 3
     75 3 1
     76 010
     77 101
     78 010
     79 ```
     80 
     81 Output
     82 
     83 ```
     84 8
     85 ```
     86 
     87 Note
     88 
     89 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)$.
     90 
     91 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$.