解題語(yǔ)言不限Java
拖更了,不好意思
- Advent of Code Day 1 逆向驗(yàn)證碼
- Advent of Code Day 2 損壞校驗(yàn)和
- Advent of Code Day 3 螺旋內(nèi)存
- Advent of Code Day 4 高熵密碼
- Advevnt of Code Day 5 曲折的蹦床迷宮
- Advent of Code Day 6 內(nèi)存重分配
- Advent of Code Day 7 遞歸馬戲團(tuán)
- Advent of Code Day 8 注冊(cè)表愛(ài)好者
- Advent of Code Day 9 流處理
- Advent of Code Day 10 結(jié)哈希
- Advent of Code Day 11 六邊形迷宮
題目?jī)?nèi)容
Crossing the bridge, you've barely reached the other side of the stream when a program comes up to you, clearly in distress. "It's my child process," she says, "he's gotten lost in an infinite grid!"
當(dāng)你快跨過(guò)橋的時(shí)候,你看見(jiàn)一個(gè)沮喪的程序走過(guò)來(lái)。“這是我孩子的路徑”,她說(shuō),“他在這個(gè)無(wú)窮大的網(wǎng)格里迷路了”。
Fortunately for her, you have plenty of experience with infinite grids.
對(duì)她來(lái)說(shuō)幸運(yùn)的是,你對(duì)無(wú)窮網(wǎng)格很有經(jīng)驗(yàn)。
Unfortunately for you, it's a hex grid.The hexagons ("hexes") in this grid are aligned such that adjacent hexes can be found to the north, northeast, southeast, south, southwest, and northwest:
對(duì)你來(lái)說(shuō)不幸的是,這是個(gè)六邊形網(wǎng)格(譯者注:我把鏈接的網(wǎng)址換成百度了,方便各位沒(méi)有梯子的同志們)。六邊形在網(wǎng)格里對(duì)齊,并且可以向北,西北,西南,東北,東南移動(dòng)。
\ n /
nw +--+ ne
/ \
-+ +-
\ /
sw +--+ se
/ s \
You have the path the child process took. Starting where he started, you need to determine the fewest number of steps required to reach him. (A "step" means to move from the hex you are in to any adjacent hex.)
你有她孩子走過(guò)的路徑,從當(dāng)前位置開(kāi)始,你需要去判斷到達(dá)他所在位置的最短路徑。
For example:
-
ne,ne,ne
is3
steps away.
最短路徑為3。 -
ne,ne,sw,sw
is0
steps away (back where you started).
最短路徑為0,因?yàn)檗D(zhuǎn)了一圈回來(lái)了。 -
ne,ne,s,s
is2
steps away (se,se
).
最短路徑為2(se,se
)。 -
se,sw,se,sw,sw
is3
steps away (s,s,sw
).
最短路徑為3(s,s,sw
)。
解題思路
這個(gè)題目是要求在原點(diǎn)到目標(biāo)點(diǎn)的曼哈頓距離。不知道曼哈頓距離的朋友可以去看看曼哈頓距離。
難點(diǎn)在于和直角坐標(biāo)系相比,移動(dòng)向量多了一,并且有移動(dòng)向量之間是可以互相轉(zhuǎn)換的。
比如說(shuō):
我用移動(dòng)向量(x,y,z)來(lái)表示六邊形網(wǎng)格中的位置。
- x是向北移動(dòng)的距離
- y是向西北移動(dòng)的距離
- z是向東北移動(dòng)的距離
那位置(-1,1,1)和位置(0,0,0)是一樣的
但是在直角坐標(biāo)系中(x,y)的組合是不會(huì)出現(xiàn)這樣的問(wèn)題。因?yàn)樵诙S平面。垂直的兩個(gè)向量是不會(huì)互相影響的。
我在犯了這個(gè)錯(cuò)誤之后借鑒了一下這個(gè)網(wǎng)站,這里面很詳細(xì)的講了各種方法來(lái)解這個(gè)問(wèn)題。我會(huì)在整個(gè)活動(dòng)結(jié)束之后出一個(gè)學(xué)習(xí)筆記來(lái)解析這個(gè)題目。