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

解題語(yǔ)言不限Java

謎題還有第二部分,不過是留給大家的,能解出第一題的,才能寫第二題

學(xué)生黨,今天課比較多,沒在晚上搞完。

題目?jī)?nèi)容

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.

一個(gè)從CPU來的緊急中斷到達(dá),CPU被困在了一個(gè)跳躍的迷宮里。它需要來著空閑幫助才可以從這個(gè)迷宮里出來。

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.

這條消息包含了一個(gè)跳躍偏移量列表。跳躍是相對(duì)的:-1 會(huì)把指針移到前一個(gè)項(xiàng),2 會(huì)跳過一個(gè)。從列表的第一個(gè)開始,到跳出最后一個(gè)為止。

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.

此外,每當(dāng)跳躍一次后,原來的偏移量會(huì)加一。所以,如果你從偏移量3起跳,你會(huì)向前跳三個(gè)然后原來的那個(gè)偏移量變成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:

正值的偏移量會(huì)向前移動(dòng),負(fù)值的偏移量會(huì)向后移動(dòng)。 為了更加方便閱讀,這些跳躍偏移量會(huì)被寫到一行里。當(dāng)前指針讀取的偏移量會(huì)用括號(hào)標(biāo)注。

  • before we have taken any steps.
    在進(jìn)行任何移動(dòng)前
    (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來做跳躍(并沒有移動(dòng))。幸運(yùn)的是, 這個(gè)偏移量會(huì)在之后加一。
    (1) 3 0 1 -3

  • step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
    因?yàn)槠屏孔兓耍灾羔槕?yīng)該向前跳1,。 然后原先的偏移量加一,現(xiàn)在是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.
在這個(gè)例子里,從

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

解題思路

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容