0
這是我的問題。我想創建一個算法,爲n維井字棋板生成每個可能獲勝棋盤狀態的數組陣列。假設你有一個N = 2板,這意味着2×2,那麼函數應該返回下面的數組:尋找所有獲勝的井字棋牌局的集合
wins = [
[1,2],
[1,3],
[1,4],
[2,4]
]
我知道這是不是專門MATLAB的問題,但是我想擴大我的理解怎麼樣MATLAB工作。我的一般想法是執行以下操作的算法:
generate an n-dimensional board of zeros
1. Go to the first cell, record that index ([1,])
2. Go to the end of the row, and that's your first board state ([1,2])
3. Go to the end of the column, that's your second board state ([1,3])
4. Go to the end of the diagonal, that's your third board state ([2,3])
5. Advance to the next cell, repeat, checking if you have already created that board state first ([2,4] should be the only one it hasn't done)
我認爲我是在過度解決問題,但我不確定如何處理它。有人能給我一些指導如何以MATLAB-y的方式做到這一點?我的猜測是遍歷矩陣,只是選擇整行/列/對角線很容易,它是'檢查它是否存在'的一部分,我沒有得到。一般來說,你會如何稱這種算法?謝謝你的幫助!