# [Usaco2005 Dec]Knights of Ni 骑士

2018-02-04 18:33:49来源:cnblogs.com作者:Wolfycz人点击

Description

Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.

Input

0：贝茜可以通过的空地1：由于各种原因而不可通行的区域2：贝茜现在所在的位置3：骑士们的位置4：长着贝茜需要的灌木的土地

Output

Sample Input

8 4

4 1 0 0 0 0 1 0

0 0 0 1 0 1 0 0

0 2 1 1 3 0 4 0

0 0 0 4 1 1 1 0

Sample Output

11

HINT

这片森林的长为8，宽为4．贝茜的起始位置在第3行，离骑士们不远．贝茜可以按这样的路线完成骑士的任务：北，西，北，南，东，东，北，东，东，南，南．她在森林的西北角得到一株她需要的灌木，然后绕过障碍把它交给在东南方的骑士．