Pat*_*ryk -2 c# stack-overflow visual-studio-2010
我已经实现了可以在此链接下找到的递归算法.当3d数组为10x10x10时,它工作得很好.
我试图让它运行200x200x200阵列然而,Visual Studio说我可能正在进行无限的恢复(我很确定我的编程没问题).有办法处理吗?我已经尝试[DebuggerNonUserCode]在递归方法之前放右,但它没有奏效.
忘了提,它是Visual Studio 2010.
这是我的程序的递归函数.我正在为每个单元格运行,标记为Unvisited.
public static int tmp_lowest_floor = 0;
public static int tmp_maks_size = 0;
static void function1(Point[, ,] array, int pos_y, int pos_z, int pos_x) // recursive function
{
Point cell = array[pos_y, pos_z, pos_x];
if (cell.Visited == false && cell.IsCave)
{
cell.Visited = true; // changing to visited so we do not count anything for this cell anymore
tmp_maks_size++; // increasing for each cell in this cave (in this run)
if (tmp_lowest_floor < pos_y) { tmp_lowest_floor = pos_y; }
cell.FillNeighbourList(array, pos_y, pos_z, pos_x);// adds neighbours into cell's list (max 6) up, down, north, east, south, west
foreach (Point p in cell.neighbours) // max 6 times recursion in here (I know it sounds horrible, but once I check all the neighbours for a cell, I'll not have to check it ever again)
{
if (p != null)
{
if (p.IsCave == true && p.Visited == false)
{
function1(tablica, p.pos_y, p.pos_z, p.pos_x);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ps我知道我可以以迭代的方式做到这一点,但是,家庭作业说它必须通过递归来完成
你不能忽视它,你真的吹了堆栈.
你做的是增加堆栈(从而延迟问题)或者意识到手头的问题是一个非常糟糕的算法实现并修复它(比如,通过使用迭代方法,或利用尾调用递归) .
顺便说一句,想要知道你用200x200x200阵列中的每个单元格使用多少堆栈来跟踪你的x,y和z坐标而不是其他东西?96MB.Windows默认情况下为您提供1MB宽的堆栈.
编辑:要解决您的具体问题,您所拥有的是一种非常基本的填充算法.它的迭代形式使用队列和列表.在伪代码中:
list visited=[];
queue q=[];
q.push(startnode); // the starting point
while(!q.empty())
{
// make sure we don't revisit nodes
if(visited.contains(node))
continue;
visited.add(node=q.pop()); // we just visited the top
// do your conditions here, the cave thing? no clue what that is
// if it passes your filter, do any extra operations on your node here (none in your code)
// and push the neighbours in the queue
q.push_all(node.get_neighbours());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1733 次 |
| 最近记录: |