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?
要走多少步才跳出列表?

解题思路

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 海,波浪如花 她,还未长大 未烫过头发 没读过蒹葭 心里的人儿 冒出了萌芽 赶不上火车的步伐 着急着要去看天下 落...
    小久英阅读 401评论 9 11
  • 明天你是否会想起,昨天你写的日记,明天你是否会惦记,曾经最爱哭的你~ 曾经,是回不去的曾经;明天,是永远也追不上的...
    亦心月阅读 215评论 0 0
  • 一圈 一圈 是寂寞的烟火 一丝 一丝 是天空的泪帘 睁开眼 是身后彩色的黑暗 闭上眼 是淌下无色的朱砂
    月亭阅读 140评论 6 3