解題語言不限Java
拖更了,不好意思
- Advent of Code Day 1 逆向驗證碼
- Advent of Code Day 2 損壞校驗和
- 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 遞歸馬戲團
- Advent of Code Day 8 注冊表愛好者
- Advent of Code Day 9 流處理
- Advent of Code Day 10 結(jié)哈希
- Advent of Code Day 11 六邊形迷宮
題目內(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)你快跨過橋的時候,你看見一個沮喪的程序走過來。“這是我孩子的路徑”,她說,“他在這個無窮大的網(wǎng)格里迷路了”。
Fortunately for her, you have plenty of experience with infinite grids.
對她來說幸運的是,你對無窮網(wǎng)格很有經(jīng)驗。
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:
對你來說不幸的是,這是個六邊形網(wǎng)格(譯者注:我把鏈接的網(wǎng)址換成百度了,方便各位沒有梯子的同志們)。六邊形在網(wǎ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.)
你有她孩子走過的路徑,從當(dāng)前位置開始,你需要去判斷到達(dá)他所在位置的最短路徑。
For example:
-
ne,ne,neis3steps away.
最短路徑為3。 -
ne,ne,sw,swis0steps away (back where you started).
最短路徑為0,因為轉(zhuǎn)了一圈回來了。 -
ne,ne,s,sis2steps away (se,se).
最短路徑為2(se,se)。 -
se,sw,se,sw,swis3steps away (s,s,sw).
最短路徑為3(s,s,sw)。
解題思路
這個題目是要求在原點到目標(biāo)點的曼哈頓距離。不知道曼哈頓距離的朋友可以去看看曼哈頓距離。
難點在于和直角坐標(biāo)系相比,移動向量多了一,并且有移動向量之間是可以互相轉(zhuǎn)換的。
比如說:
我用移動向量(x,y,z)來表示六邊形網(wǎng)格中的位置。
- x是向北移動的距離
- y是向西北移動的距離
- z是向東北移動的距離
那位置(-1,1,1)和位置(0,0,0)是一樣的
但是在直角坐標(biāo)系中(x,y)的組合是不會出現(xiàn)這樣的問題。因為在二維平面。垂直的兩個向量是不會互相影響的。
我在犯了這個錯誤之后借鑒了一下這個網(wǎng)站,這里面很詳細(xì)的講了各種方法來解這個問題。我會在整個活動結(jié)束之后出一個學(xué)習(xí)筆記來解析這個題目。