How can I shift and merge elements of a matrix to have the following result ?移位和合並元件
向右移動: [[0,0,2,2,0,2],[8,4,2,2,0,2]]
==>[[0,0,0,0,4,2],[0,0,8,4,4,2]]
或
左移: [[0,0,2,2,0,2],[8,4,2,2,0,2]]
==>[[4,2,0,0,0,0],[8,4,4,2,0,0]]
It's like the 2048 game. For example, when the user do a left move, every numbers go to the left of the list and if 2 numbers side-by-side are equals, tere is an addition of the two numbers.
我想用循環來做到這一點。
我嘗試了一些我在互聯網上找到的代碼,但作爲開發者,我沒有找到明白如何做到這一點的簡單代碼。
在此先感謝您的幫助。
請解釋你正在嘗試做什麼。你的輸入和輸出沒有明顯的模式。 (而且我注意到你已經爲同樣的輸入提供了兩個樣本輸出。) –
感謝您的建議。我改進了我的解釋。 :) – titi157
是的,就是這樣。 :) – titi157