我有一個算法,我想找出它的複雜性,但有遞歸,我不知道如何計算遞歸。我的代碼是:遞歸算法的複雜性
public boolean algorithm(int x, int y) {
if (x == matrixHeight - 1 && matrix1[x][y] == '0') {
return true;
} else if (x == 1 && matrix1[x-1][y] == '0') {
return true;
} else if (y == matrixWidth - 1 && matrix2[x][y] == '0') {
return true;
} else if (y == 1 && matrix2[x][y-1] == '0') {
return true;
}
if (matrix1[x-1][y] == '0' && tempMatrix[x-1][y] == '-'){
path.push(new int[]{x-1, y});
tempMatrix[x-1][y] = '+'
if (!algorithm(x-1, y)) {
path.pop();
} else {
return true;
}
}
if (matrix2[x][y] == '0' && tempMatrix[x][y+1] == '-'){
path.push(new int[]{x, y+1});
tempMatrix[x][y+1] = '+';
if (!algorithm(x, y+1)) {
path.pop();
} else {
return true;
}
}
if (matrix1[x][y] == '0' && tempMatrix[x+1][y] == '-'){
path.push(new int[]{x+1, y});
tempMatrix[x+1][y] = '+';
if (!algorithm(x+1, y)) {
path.pop();
} else {
return true;
}
}
if (matrix2[x][y-1] == '0' && tempMatrix[x][y-1] == '-'){
path.push(new int[]{x, y-1});
tempMatrix[x][y-1] = '+';
if (!algorithm(x, y-1)) {
path.pop();
} else {
return true;
}
}
return false;
}
- 在那裏,
x
,y
在矩陣座標。 matrix1
和matrix2
是包含'0'
或'1'
tempMatrix
二維陣列由包含 '+' 或二維陣列 ' - 'path
是堆棧matrixHeight
是matrix1.length
matrixWidth
是matrix[0].length
N
,M
是矩陣的大小(constan t)
注意:這是使用回溯的迷宮解算器。
此代碼需要註釋。 – Brad 2011-04-21 19:41:46
如果您可以簡要說明此代碼的作用以及如何執行此操作,那將會很有幫助。 – MAK 2011-04-21 19:57:03
此代碼不需要註釋,它只需要有意義的名稱。 – 2011-04-21 23:52:57