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

解題思路

這一題比較簡單,只要讀取完所有的數值之后,將數值放入數組中。然后按照題目里說的一個個值操作就好了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 海,波浪如花 她,還未長大 未燙過頭發 沒讀過蒹葭 心里的人兒 冒出了萌芽 趕不上火車的步伐 著急著要去看天下 落...
    小久英閱讀 399評論 9 11
  • 明天你是否會想起,昨天你寫的日記,明天你是否會惦記,曾經最愛哭的你~ 曾經,是回不去的曾經;明天,是永遠也追不上的...
    亦心月閱讀 215評論 0 0
  • 一圈 一圈 是寂寞的煙火 一絲 一絲 是天空的淚簾 睜開眼 是身后彩色的黑暗 閉上眼 是淌下無色的朱砂
    月亭閱讀 138評論 6 3