我有一个算法,我想找出它的复杂性,但有递归,我不知道如何计算递归。我的代码是:递归算法的复杂性
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