在c中实现3D数组的递归算法
我需要实现一个具有以下签名的函数:
int rec(int board[SIZE][SIZE][10])
* SIZE 是一个常量。
该函数应该是递归的。 板本身是二维的,第三维用于保存单元格的可能值。
我的问题是:如何在函数不接受当前板的行和列作为参数的情况下递归扫描板?
I need to implement a function with the following signature:
int rec(int board[SIZE][SIZE][10])
* SIZE is a const.
The function should be recursive.
The board itself is 2 dimensional, and the 3rd dimension is used for holding the cell's possible values.
My question is: how can i scan the board recursivly, without the function accepting the current row and column of the board as parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的函数实际上必须是递归的吗?您知道这个数组只有 3 个维度,因此从头到尾扫描整个数组的算法只需 3 个嵌套 for 循环即可实现(2 个用于标识当前单元格,一个用于迭代中的所有 10 个值)细胞)。
关于这一点,当您说“保存单元格的可能值”时,该单元格实际上是否有 10 个需要跟踪的独立整数?相反,如果它只是一个具有 10 个可能的不同值的 int,则数组的第三个维度是不必要的。虽然可能有 10 个可能的值,但单元格一次只能保存其中一个。
Does your function actually have to be recursive? You know there's only 3 dimensions to this array, so an algorithm that scans the entire array from start to finish can be implemented with nothing more than 3 nested for loops (2 to identify the current cell, and one to iterate over all 10 values in the cell).
On that note, when you say "holding the cell's possible values", does this cell actually have 10 separate ints that you need to track? If instead it's simply an int that has 10 possible distinct values, then that third dimension to the array is unnecessary. While there may be 10 possible values, the cell can only hold one of them at a time.