Advevnt of Code Day 5 曲折的蹦床迷宫

解题语言不限Java

谜题还有第二部分,不过是留给大家的,能解出第一题的,才能写第二题

学生党,今天课比较多,没在晚上搞完。

题目内容

An urgent interrupt arrives from the CPU: it’s trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit.

一个从CPU来的紧急中断到达,CPU被困在了一个跳跃的迷宫里。它需要来着空闲帮助才可以从这个迷宫里出来。

The message includes a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.

这条消息包含了一个跳跃偏移量列表。跳跃是相对的:-1 会把指针移到前一个项,2 会跳过一个。从列表的第一个开始,到跳出最后一个为止。

In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.

此外,每当跳跃一次后,原来的偏移量会加一。所以,如果你从偏移量3起跳,你会向前跳三个然后原来的那个偏移量变成4。

For example, consider the following list of jump offsets:

比如说,看下这段跳跃偏移量列表:

0
3
0
1
-3

Positive jumps (“forward”) move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:

正值的偏移量会向前移动,负值的偏移量会向后移动。 为了更加方便阅读,这些跳跃偏移量会被写到一行里。当前指针读取的偏移量会用括号标注。

  • before we have taken any steps.
    在进行任何移动前
    (0) 3 0 1 -3

  • jump with offset 0 (that is, don’t jump at all). Fortunately, the instruction is then incremented to 1.
    以偏移量0来做跳跃(并没有移动)。幸运的是, 这个偏移量会在之后加一。
    (1) 3 0 1 -3

  • step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
    因为偏移量变化了,所以指针应该向前跳1,。 然后原先的偏移量加一,现在是2。
    2 (3) 0 1 -3

  • jump all the way to the end; leave a 4 behind.
    跳到列表末尾,原偏移量变成4。
    2 4 0 1 (-3)

  • go back to where we just were; increment -3 to -2.
    返回原来的位置,末尾的偏移量从-3变成-2。
    2 (4) 0 1 -2

  • jump 4 steps forward, escaping the maze.
    跳向前跳四步,跳出列表
    2 5 0 1 -2

In this example, the exit is reached in 5 steps.
在这个例子里,从

How many steps does it take to reach the exit?
要走多少步才跳出列表?

解题思路

这一题比较简单,只要读取完所有的数值之后,将数值放入数组中。然后按照题目里说的一个个值操作就好了。

    原文作者:宅纸箱
    原文地址: https://www.jianshu.com/p/ce7e2f8b74c3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞