我已經實現了遞歸算法,可以在這個link下找到。 當3d數組爲10x10x10時,它工作得很好。如何強制Visual Studio忽略'System.StackOverflowException'?
我試圖讓它運行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);
}
}
}
}
}
p.s. 我知道我可以用迭代的方式做,但是作業說它必須用遞歸來完成
您不能忽略StackOverflowException。非遞歸地實現算法。 –
StackOverflow是一個錯誤,而不是一個警告。你不能忽視。 – SLaks
不僅你不能忽視這一點,現在你應該永遠。 – MyCodeSucks