Brute force

TimeLimit:1000MS  MemoryLimit:128MB
64-bit integer IO format:%lld
未提交 | 登录后收藏
Problem Description

Given an n*n grid matrix with rows and columns numbered from 1 to n, the grid coordinates of row i and column j are (i,j)

Each square is either flat or a trap.

The traveler starts in squares (r1, c1) and goes to squares (r2, c2).

The starting point and destination are guaranteed to be flat ground.

The traveler can move up, down, left, or right one space at a time.

We make sure the traveler doesn't step out of the matrix or into a trap while moving.

But traveler may not make it to his destination because of traps.

Fortunately, he can choose any two flat squares and build a teleporter array on them.

Teleport array allows traveler to teleport freely between two flat squares.

However, building a transport array is a significant cost.

The cost of establishing a transmission array at squares (rs, cs) and (rt,ct) is (rs − rt)2+(cs − ct)2.

Calculate the minimum cost of establishing a teleport array to enable travelers to successfully reach their destination.

If traveler can walk directly to his destination without having to build a teleport array, the cost is zero.

Note that no more than one transport array can be established.

Input

The first line contains the integer n, representing the size of the matrix.

The second row contains two integers r1 and c1, indicating that the starting grid coordinates are (r1,c1).

The third row contains two integers, r2,c2, indicating that the destination grid coordinates are (r2,c2).

The next n lines, each containing a string of '0' or '1' of length n, where the jth character in line i is 0, indicating that square (i,j) is flat, and 1, indicating that square (i,j) is a trap.

(1≤n≤50,1≤r1,c1,r2,c2≤n。)

Output

Output an integer that represents the minimum cost required to ensure that the trip will succeed.

SampleInput
3
1 1
3 3
011
000
100
SampleOutput
0
Submit
题目统计信息详细
总AC数18
通过人数15
尝试人数15
总提交量21
AC率71.43%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签
作者

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: