【发布时间】:2017-09-20 16:39:45
【问题描述】:
这是我的问题。我想创建一个算法,它为 n 维井字棋盘生成每个可能的获胜棋盘状态的数组数组。假设您有一个 n = 2 的板,即 2x2,那么该函数应返回以下数组:
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 的方式做到这一点吗?我的猜测是遍历矩阵并只选择整行/列/对角线很容易,这是我没有得到的“检查它是否存在”部分。一般来说,你会如何称呼这个算法?感谢您的帮助!
【问题讨论】: