编程介的小学生 2017-03-29 09:48 采纳率: 20.5%
浏览 725
已采纳

Fly Flies

A fly is flying on a gridding. Stupid enough, it always flies in the direction "up, left, down, right, up, left, down, right, up, ...". In every step, the fly can choose to move in the specified direction, or to stay still. Each grid has its property, indicated by an integer:

A "-1" indicates that the grid is a barrier where the fly can not enter.
A "0" indicates the grid has nothing in.
A positive integer n indicates a trap in the grid which will hold the fly for the following n seconds. Although the fly can turn its direction, it can't move a bit.
Given the fly's starting position, you shall tell at least how many steps the fly needs to arrive the given target position.

Input

There are multiple testcases.

Each testcase begins with a line with the grids' width W and height H (1 <= W, H <= 20), the starting position (in row and column), the ending position (in row and column). You may assume that the starting position is always valid, that is, it will not be a "-1" grid.

Then H lines follows, each have W integers, indicating the grids.

Output

For each testcase, output one line containing the minimum steps. If it's impossible for the fly to arrive the target position, output -1 instead.

Sample Input

2 2 1 0 0 1
0 0
0 0

2 2 1 0 0 1
-1 0
0 -1

Sample Output

4
-1

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-07 14:57
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!