Ghost Leg (aka Amidakuji) is a tiny game which is used to create random pairings between two arrays. This is commonly used in lucky draws.
A ghost leg consists of vertical lines and horizontal lines. If there are N elements in an array, there would be N vertical lines and any number of horizontal lines. Horizontal lines should be touching two adjacent vertical lines. The rule of matching is by picking a vertical line at the top, and go downwards until you hit a horizontal line. Whenever you hit a horizontal line, travel along that horizontal line. Then continue to go downwards and repeat this process until you reach the other end.
Consider this example on the left.
Obviously when there are no horizontal lines at all, the matched pairs would be {A,1}, {B,2} and {C,3}.
With the horizontal lines added, the matched pairs become {A,3}, {B,2} and {C,1}.
Make sure you understand this example before proceeding to the main question :)
Here comes the question. Consider the case N = 10. Given that when there are no horizontal lines, the matching pairs are {A,1}, {B,2}, {C,3}, {D,4}, {E,5}, {F,6}, {G,7}, {H,8}, {I,9}, {J,10}. What is the minimum number of horizontal lines to be added such that the final outcome would be {A,7}, {B,4}, {C,3}, {D,9}, {E,2}, {F,8}, {G,1}, {H,6}, {I,10}, {J,5}?
Please submit your answer through this link.
畫鬼腳 是一個用以將兩個序列配對的小遊戲。畫鬼腳被經常應用在抽獎之中。
鬼腳圖由直線及橫線組成。若一序列有N個項目,該鬼腳圖則共有N條直線及任何數量的橫線,且橫線必須連著相鄰的直線。配對規則為選取其中一個頂點,然後向下移動,直至遇到橫線。遇到橫線的時候需要跟著橫線行走,然後繼續向下移動,並重覆以上步驟,直至到達另一端。
試參考左側例子。
當圖中完全沒有橫線,明顯地配對結果為 {A,1}、{B,2}及{C,3}。
加上橫線後,配對結果便變成 {A,3}、{B,2}及{C,1}。
繼續閱讀問題前請確保你已完全明白這個例子 :)
現在終於來到問題部分了。考慮N = 10。已知當鬼腳圖中完全沒有橫線,配對結果為 {A,1}, {B,2}, {C,3}, {D,4}, {E,5}, {F,6}, {G,7}, {H,8}, {I,9}, {J,10}。求最少需要加上的橫線數目使得配對結果為 {A,7}, {B,4}, {C,3}, {D,9}, {E,2}, {F,8}, {G,1}, {H,6}, {I,10}, {J,5}。
請經此連結提交答案。